mongodb search array code example

Example 1: lookup in mongodb array

> db.demo396.aggregate([
...    { "$lookup": {
...       "from": "demo395",
...       "let": { "details": "$details" },
...       "pipeline": [
...          { "$match": { "$expr": { "$in": [ "$_id", "$$details" ] } } }
...       ],
...       "as": "output"
...    }}
... ])

Example 2: mongodb find array with element

// Finds all documents that have a property named "tags"
// which has at least one array element matching "red"
db.inventory.find( { tags: "red" } )

Example 3: lookup in mongodb array

{ "_id" : ObjectId("5e5e787817aa3ef9ab8ab209"), "details" : [ ObjectId("5e5e782317aa3ef9ab8ab207"), ObjectId("5e5e782317aa3ef9ab8ab208") ], "output" : [ { "_id" : ObjectId("5e5e782317aa3ef9ab8ab207"), "Name" : "Chris" }, { "_id" : ObjectId("5e5e782317aa3ef9ab8ab208"), "Name" : "David" } ] }

Tags:

Misc Example