Search code examples
mongodbcommand-linemongodump

mongodump 1 single document fails because ObjectId


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)?


Solution

  • 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"}}'