[SERVER-11755] findAndModify with compound index failed on cluster (sharding) Created: 18/Nov/13  Updated: 30/Dec/13  Resolved: 18/Nov/13

Status: Closed
Project: Core Server
Component/s: Querying, Sharding
Affects Version/s: 2.4.6
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Ofer Cohen Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
Operating System: ALL
Steps To Reproduce:

> db.bcs.insert({sid:111,bmonth:"201401"});

The shard key is sid and bmonth.

> db.bcs.findAndModify({sid:111,bmonth:"201401"},{$inc:{some_field : 1}});
Sun Nov 17 13:26:31.118 findAndModifyFailed failed: {
    "code" : 13343,
    "ok" : 0,
    "errmsg" : "exception: query for sharded findAndModify must have shardkey"
} at src/mongo/shell/collection.js:399

> db.runCommand( { findAndModify: "bcs", query: { sid: 111, bmonth:"201401" }, update: { $inc: { some_field: 1 } } } );
{
    "value" : {
        "_id" : ObjectId("5288a7d8eb982898248ca83a"),
        "sid" : 111,
        "bmonth" : "201401"
    },
    "lastErrorObject" : {
        "updatedExisting" : true,
        "n" : 1
    },
    "ok" : 1
}

Participants:

 Description   

Updating an existing document using findAndModify on shared cluster, when the collection have compound index raise an error: "query for sharded findAndModify must have shardkey". However, with runCommand it works just fine.



 Comments   
Comment by Scott Hernandez (Inactive) [ 18/Nov/13 ]

The problem here is that findAndModify takes a document with the fields "query", "update", etc. and not just two positional arguments. That means you essentally provided no query not update for findAndModify, leading to no shard key being in the empty query.

http://docs.mongodb.org/manual/reference/method/db.collection.findAndModify/#db.collection.findAndModify

Generated at Thu Feb 08 03:26:41 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.