Estoy tratando de extraer datos de 2 tipos de mi documento BSON usando mangosta. Los 2 datos son;
Para la intención 1, lo intenté;
db.find( { "fileID": "0pdn3jdndndj3msms, "items.tagNo": 2 }, { "items": { "$elemMatch": { "tagNo": 2 } } } );y cansé la consulta a continuación para la intención 2
db.find( { "fileID": "0pdn3jdndndj3msms, "items.tagNo": 2 }, { "items": { "$elemMatch": { "tagNo": 2, "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM", } } } );Sigo recuperando todo el objeto.
Mi documento BSON es;
{ "_id": "ID_GOES_HERE", "fileID": "0pdn3jdndndj3msms", "fileName": "Item List", "items": [ { "tagNo": 2, "status": 0, "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM", "tagUnit": 5 }, { "tagNo": 2, "status": 0, "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM", "tagUnit": 8 }, { "tagNo": 2, "status": 0, "tagID": "pOawOURVpz1IIjoQ2fhCvy7w3", "tagUnit": 81 }, { "tagNo": 4, "status": 0, "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM", "tagUnit": 904 }, { "tagNo": 3, "status": 0, "tagID": "pOawOURVpz1IIjoQ2fhCvy7w3", "tagUnit": 24 }, { "tagNo": 2, "status": 0, "tagID": "pOawOURVpz1IIjoQ2fhCvy7w3", "tagUnit": 35 }, { "tagNo": 1, "status": 0, "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM", "tagUnit": 11 }, { "tagNo": 2, "status": 0, "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM", "tagUnit": 30 } ] }Necesita un marco de agregación
db.collection.aggregate([ { $match: { //Match condition to get the matching arrays "fileID": "0pdn3jdndndj3msms", "items.tagNo": 2 } }, { $project: { "items": { $filter: { //Project only matching array elements input: "$items", as: "item", cond: { $eq: [ "$$item.tagNo", 2 ] } } } } } ]) db.collection.aggregate([ { $match: { "items": { "$elemMatch": { "tagNo": 2, "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM", } } } }, { $project: { "items": { $filter: { input: "$items", as: "item", cond: { "$and": [ { $eq: [ "$$item.tagNo", 2 ] }, { $eq: [ "$$item.tagID", "kLawOURVpz1IIjoQ2fhCvy7NM" ] } ] } } } } } ])