0

I'd like to migrate a DB from a replica set to another which belongs to a sharded cluster. Till now, this cluster has got only 1 shard, and no collection or database is really sharded.

I mongodumped and restored this DB. From one of the sharded cluster's mongos, I can run a simple query like find() but something including a projection like the following doesn't work anymore:

"find({"var1":subvar},{"_id":0, "fld1":1, "fld2":1, "fld3":1}).limit(1).sort("_id",pymongo.DESCENDING)"

I get the following error message:

database error: have to have sort key in projection and removing it

It has to be a problem related to the sharding because I restored this DB to a dev standalone mongodb server and the test ran successfully.

Thanks in advance,

Greg.

Icu
  • 1,425
  • 3
  • 16
  • 25

1 Answers1

2

This isn't a problem with with mongorestore. If you create a new collection in a sharded database in 2.2+ you will see the same behavior.

mongos> db.version()
2.2.6
mongos> db.foo.save({id:3, value:'c'})
mongos> db.foo.save({id:2, value:'b'})
mongos> db.foo.save({id:1, value:'a'})
mongos> db.foo.find({}, {id:0}).sort({id:1})
error: {
        "$err" : "have to have sort key in projection and removing it",
        "code" : 13431
}

Apparently the behavior was different in 2.0.

mongos> db.version()
2.0.9
mongos> db.foo.save({id:3, value:'c'})
mongos> db.foo.save({id:2, value:'b'})
mongos> db.foo.save({id:1, value:'a'})
mongos> db.foo.find({},{id:0}).sort({id:1})
{ "_id" : ObjectId("523bd35dff9a7c468936efba"), "value" : "a" }
{ "_id" : ObjectId("523bd358ff9a7c468936efb9"), "value" : "b" }
{ "_id" : ObjectId("523bd352ff9a7c468936efb8"), "value" : "c" }

I opened SERVER-10381 to see whether or not this is expected if you want to follow it.

EDIT: This is expected. When the query is run through mongos, the key is expected to exist so that the results from the individual shards can be merged and sorted. Prior to 2.2, the logic for a single shard/unsharded collection was apparently different, but it was changed for the sake of consistency.

Documentation will be updated to reflect his restriction.

jeffl
  • 136
  • 3