0

I'm trying to create a mongo dump for 1 single document inside a mongo collection.

When I do this on windows command line:

mongodump /host:x.x.x.x /port:27017 /username:my_user /password:my_pass -d my_db -o C:\fsdump -c "fs.files" -q '{_id: ObjectId("28ad7bkjia3e927d690385ec")}'

I get this error:

positional arguments not allowed: [ObjectId(28ad7bkjia3e927d690385ec)}']

when I change the id in mongo from ObjectId("28ad7bkjia3e927d690385ec") to "28ad7bkjia3e927d690385ec", and when I dump like this:

mongodump /host:x.x.x.x /port:27017 /username:my_user /password:my_pass -d my_db -o C:\fsdump -c "fs.files" -q '{_id: "28ad7bkjia3e927d690385ec"}'

then it works as expected

so my question is how can I use mongodump and do filtering on specific ObjectId's ?

or is there another way to create an export for a subset of documents in a collection (instead of the entire collection)?

TomTem
  • 89
  • 9

1 Answers1

1

ObjectId("28ad7bkjia3e927d690385ec") is a javascript function call to the ObjectId constructor. This is not valid JSON. "28ad7bkjia3e927d690385ec" is also not a valid ObjectId.

Mongodump uses an extended form of JSON, which had a tag to indicate the field type, so you would specify an ObjectId like this:

-q '{"_id":{"$oid":"5f76b7cc0311bd14f80a3dec"}}'

Joe
  • 25,000
  • 3
  • 22
  • 44
  • I didn't know about $oid, it works, thx! (btw on windows I also had to escape everything using double quotes like this: `-q "{\"_id\":{\"$oid\":\"5cf923a14e401624b099f4ce\"}}"`) – TomTem Feb 12 '20 at 17:38