mongodbmongoexport

Mongodb Export Query using Wildcards?


I have a mongo collection with documents with the following structure:

{
    "_id": ObjectId("5db82cd948d57f69b83b5e59"),
    "sensor": {
        "_id": "123123",
        "type": "typeA"
    },
    "Object": {
        "_id": "lkj123",
        "type": "typeB"
    }
}

I need to export all documents for a list of sensor id's, but the corresponding sensor types are not included.

The following would work for 2 documents, but this would require me to first look up the corresponding types and the list of sensor id's are too long to do this manually.

mongoexport --uri="<MY-CONNECTION-STRING>" --collection="<MY-COLLECTION>" --out=MY-EXPORT.json -q="{$or:[{sensor:{_id : '123', type:'typeA'}},{sensor:{_id : 'abc', type:'typeB'}}]}"

I have tried to use wildcards for the sensor types, but I couldn't get it working properly.

How can I create a working query that allows me to just copy past the sensorid's in to do the trick?

Thanks in advance!


Solution

  • Your query will work when you only pass sensor id, just pass them in this format

    mongoexport --uri="<MY-CONNECTION-STRING>" --collection="<MY-COLLECTION>" --out=MY-EXPORT.json -q="{$or:[{'sensor._id':'123'},{'sensor._id':'abc'}]}"
    

    And if you have all the sensor's id in an array, I would prefer to use $in like below,

    mongoexport --uri="<MY-CONNECTION-STRING>" --collection="<MY-COLLECTION>" --out=MY-EXPORT.json -q="{'sensor._id': {$in: ['123', 'abc']}}"
    

    Both examples will work for you