How to find documents in MongoDb matching a field and subdocument field that are in an array

JPacheco

The document structure is as follows:

{
"_id" : "V001-99999999",
"vendor_number" : "V001",
"created_time" : ISODate("2016-04-26T22:15:34Z"),
"updated_time" : ISODate("2016-06-07T21:45:46.413Z"),
"items" : [
    {
        "sku" : "99999999-1",
        "status" : "ACTIVE",
        "listing_status" : "LIVE",
        "inventory" : 10,
        "created_time" : ISODate("2016-05-14T22:15:34Z"),
        "updated_time" : ISODate("2016-05-14T20:42:21.753Z"),
    },
    {
        "sku" : "99999999-2",
        "status" : "INACTIVE",
        "listing_status" : "LIVE",
        "inventory" : 10,
        "created_time" : ISODate("2016-04-26T22:15:34Z"),
        "updated_time" : ISODate("2016-06-06T20:42:21.753Z"),
    }
]

}

I want to obtain the sku from the item, the conditions are:

1) "vendor_number" = "XXX"

2) items.status = "ACTIVE" AND items.updated_time < [given_date]

Result example:

"sku" : "99999999-2" 

or csv:

"sku","99999999-2"

Thank you for your support.

Hayden Braxton

This should be what you want. Although I'm assuming you wanted "status": "active"?

db.getCollection('collection').aggregate([

 { $match: { "vendor_number": "XXXX" } },

 { $project: {
     "items": {
         $filter: {
            input: "$items",
            as: "item",
            cond: { $eq: ["$$item.status", "ACTIVE"] } // or maybe ["$$item.listing_status", "LIVE"] ?
         }
      }
    }
  },     

 { $project: { "items.sku": true } }

])

I love using aggregation to manipulate stuff. It's great all the things you can do with it. So here's what's going on:

The first part is simple. The $match step in the aggregation pipeline says just give me documents where vendor_number is "XXXX".

The next part is a bit hairy. The first projection step creates a new field, called "items", I could have called it "results" or "bob" if I wanted to. The $filter specifies which items should go into this new field. The new "items" field will be an array that will have all the results from the previous items field, hence the input: "$items", where you're using the keyword "item" to represent each input item that comes into the filter. Next, the condition says, for each item, only put it in my new "items" array if the item's status is "ACTIVE". You can change it to ["$$items.listing_status", "LIVE"] if that's what you needed. All of this will pretty much give you you're result.

The last project just get's rid of all other fields except for items.sku in each element in the new "items" array.

Hope this help. Play around with it and see what else you can do with the collection and aggregation. Let me know if you need any more clarification. If you haven't used aggregation before, take a look at the aggregation docs and the list of pipeline operators you can use with aggregation. Pretty handy tool.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Exclude field in array of subdocument in mongodb

From Dev

Exclude field in array of subdocument in mongodb

From Dev

How to update a field in an array's subdocument contained in an array's subdocument in MongoDB using C# driver?

From Dev

How to update a field in an array's subdocument contained in an array's subdocument in MongoDB using C# driver?

From Dev

Return documents matching field array

From Dev

upsert a field in a subdocument in an array by index in MongoDB

From Dev

How to dynamically $set a subdocument field in mongodb?

From Dev

How to use $toString in $project for subdocument field on MongoDB?

From Dev

How to use $toString in $project for subdocument field on MongoDB?

From Java

How to find all documents that have a matching field inside an embbedded list

From Dev

Mongodb Indexing array field in the documents

From Dev

Find documents whose array field contain some subsets in MongoDB

From Dev

MongoDB, find field into array

From Dev

how do I find all documents with a field that is NaN in MongoDB?

From Dev

how do I find all documents with a field that is NaN in MongoDB?

From Dev

MongoDB how to find documents by searching a Mixed Schema field?

From Dev

How to find documents in mongodb collection which has a field with 2 values?

From Dev

Find Subdocument in Array with mongodb

From Dev

How to retrieve MongoDB documents via selection by array field

From Dev

Mongodb aggregate for nested subdocument field

From Dev

In Mongodb subdocument array is there any way to add a new field in each subcoument

From Dev

find subdocument by id inside a array field of nested object

From Dev

How to find documents where field1 is greater than field2? ( Nodejs/Mongoose/MongoDB)

From Dev

How to find documents where field1 is greater than field2? ( Nodejs/Mongoose/MongoDB)

From Dev

How to find document and single subdocument matching given criterias in MongoDB collection

From Dev

finding documents in mongodb by array for one field?

From Dev

MongoDB How to get value from subdocument when field name is unknown

From Dev

How to search in every field of subdocument

From Dev

How to search in every field of subdocument

Related Related

  1. 1

    Exclude field in array of subdocument in mongodb

  2. 2

    Exclude field in array of subdocument in mongodb

  3. 3

    How to update a field in an array's subdocument contained in an array's subdocument in MongoDB using C# driver?

  4. 4

    How to update a field in an array's subdocument contained in an array's subdocument in MongoDB using C# driver?

  5. 5

    Return documents matching field array

  6. 6

    upsert a field in a subdocument in an array by index in MongoDB

  7. 7

    How to dynamically $set a subdocument field in mongodb?

  8. 8

    How to use $toString in $project for subdocument field on MongoDB?

  9. 9

    How to use $toString in $project for subdocument field on MongoDB?

  10. 10

    How to find all documents that have a matching field inside an embbedded list

  11. 11

    Mongodb Indexing array field in the documents

  12. 12

    Find documents whose array field contain some subsets in MongoDB

  13. 13

    MongoDB, find field into array

  14. 14

    how do I find all documents with a field that is NaN in MongoDB?

  15. 15

    how do I find all documents with a field that is NaN in MongoDB?

  16. 16

    MongoDB how to find documents by searching a Mixed Schema field?

  17. 17

    How to find documents in mongodb collection which has a field with 2 values?

  18. 18

    Find Subdocument in Array with mongodb

  19. 19

    How to retrieve MongoDB documents via selection by array field

  20. 20

    Mongodb aggregate for nested subdocument field

  21. 21

    In Mongodb subdocument array is there any way to add a new field in each subcoument

  22. 22

    find subdocument by id inside a array field of nested object

  23. 23

    How to find documents where field1 is greater than field2? ( Nodejs/Mongoose/MongoDB)

  24. 24

    How to find documents where field1 is greater than field2? ( Nodejs/Mongoose/MongoDB)

  25. 25

    How to find document and single subdocument matching given criterias in MongoDB collection

  26. 26

    finding documents in mongodb by array for one field?

  27. 27

    MongoDB How to get value from subdocument when field name is unknown

  28. 28

    How to search in every field of subdocument

  29. 29

    How to search in every field of subdocument

HotTag

Archive