Search code examples
mongodbmongoexport

Mongodb export with conditional logic?


I want to export "street" : "Downstreet 34"
But dont export, if the source value is other than 3

Sample 1 JSON
"addresses" : [ {"source" : 3 , "street" : "Downstreet 34"}]
Export "street" : "Downstreet 34"


Sample 2 JSON
"addresses" : [ {"source" : 2 , "street" : "Downstreet 34"}]
Dont export "street" : "Downstreet 34"


Solution

  • db.collection.find(
        { source: 2 },
        { street: 1}
    )
    

    Example that you can use to build queries like these are : source

    # SQL QUERY
    SELECT user_id, status
    FROM users
    WHERE status = "A"
    
    
    #mongoDB Query
    db.users.find(
        { status: "A" },
        { user_id: 1, status: 1, _id: 0 }
    )