These are chat archives for Automattic/mongoose

3rd
Feb 2018
Anjan Das
@bugToaster
Feb 03 2018 06:10

{
"_id" : ObjectId("5a6eca3f95332b22f8a09a63"),
"order_number" : "1",
"item" : [
{
"item_id" : "wdrghh323rgfrg444",
"item_cat_id" : "sdasdasdasd3e333",
"item_qty" : 4.0,
"item_price" : 400.0,
"item_total_price" : 1600.0,
"order_date_time" : "sdsadasd"
},
{
"item_id" : "wdrghwewr5gfrg2323",
"item_cat_id" : "sdas33sd3e333",
"item_qty" : 1.0,
"item_price" : 600.0,
"item_total_price" : 600.0,
"order_date_time" : "sdsadasd"
},
{
"item_id" : "wdrgh5yyhh23",
"item_cat_id" : "sdasdas3ed333aa",
"item_qty" : 5.0,
"item_price" : 200.0,
"item_total_price" : 1000.0,
"item_order_date_time" : "sdsasadasd"
}
],
"sub_total" : 3200.0,
"person" : [
{
"person_id" : "1st",
"person_name" : "Demo1",
"item_id" : "wdrgh5yyhh23",
"item_qty" : 3.0
},
{
"person_id" : "1st",
"person_name" : "Demo1",
"item_id" : "wdrghh323rgfrg444",
"item_qty" : 2.0
},
{
"person_id" : "2nd",
"person_name" : "Demo2",
"item_id" : "wdrgh5yyhh23",
"item_qty" : 2.0
},
{
"person_id" : "2nd",
"person_name" : "Demo2",
"item_id" : "wdrghh323rgfrg444",
"item_qty" : 2.0
},
{
"person_id" : "3rd",
"person_name" : "Demo3",
"item_id" : "wdrghwewr5gfrg2323",
"item_qty" : 1.0
}
],
"due_flag" : "1",
"given_amount" : 2000.0,
"change_amount" : 120.0,
"due_person" : "Person Demo",
"waiter_id" : "erwern432rnwefwef",
"table_id" : "sdee343rfddfegffgf",
"order_create_date_time" : "sdsdasdsa"
}

this is my demo JSON data and my query is
db.order.find( { "person": { person_name: "Demo1"} } )
but it returns null

Kev
@lineus
Feb 03 2018 10:41
@bugToaster you can try this: db.order.find({ person: { $elemMatch: { person_name: 'Demo1' }}})
you need elemMatch because you are searching within an array ( person ) in this case.
Kev
@lineus
Feb 03 2018 12:47
a more useful example might be using aggregate to find all of the orders where 'Frank' was involved:
> db.getCollection('order').aggregate([{ $match: { 'person.person_name': 'Frank' } }, {$project: { order_number: 1, _id: 0}}])
{ "order_number" : "1" }
{ "order_number" : "3" }
>