Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to get single element from nested array mongoose

//This solution isn't very elegant, but it works.

const objectInNestedArray = (await Schema.findOne({
    _id: '62a5bc408a97563a666c63dd',
    nestedArray : { $elemMatch : { _id: new mongoose.Types.ObjectId(id) }}//must be ObjectId
},
{
    nestedArray: {
        "$filter": {
            "input": "$nestedArray",
            "as": "objectInNestedArray",
            "cond": { "$eq": ["$$objectInNestedArray._id", new mongoose.Types.ObjectId(id)] } //must be ObjectId
        }
    }
}))['nestedArray'][0]
Comment

Query nested array of objects in mongoose

db.collection.find({
  "instock.items": {
    $elemMatch: {
      name: "alexa"
    }
  }
})
Comment

PREVIOUS NEXT
Code Example
Javascript :: turn Iterator into array JS 
Javascript :: extract uppercase words nodejs 
Javascript :: jquery insert after element 
Javascript :: fetch method in js 
Javascript :: javascript group by key 
Javascript :: how to change color on js 
Javascript :: js clear all select options 
Javascript :: loop through javascript object 
Javascript :: javascript log html element as dom object 
Javascript :: nuxt emit 
Javascript :: jquery get by name 
Javascript :: Obtain smallest value from array of objects in Javascript 
Javascript :: react text input onchange 
Javascript :: js get last element of array 
Javascript :: jquery fade out 
Javascript :: rounding number to x decimals javascript 
Javascript :: url regular expression 
Javascript :: hmget in redis 
Javascript :: add text to string javascript 
Javascript :: js set canvas size 
Javascript :: jquery map 
Javascript :: add query parameter to url react router v6 
Javascript :: include other js files in a js file 
Javascript :: checkbox set checked jquery 
Javascript :: pushing element in array in javascript 
Javascript :: To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect 
Javascript :: Javascript replace div content onclick a button 
Javascript :: js is of type array 
Javascript :: export all functions 
Javascript :: ruby hash to json 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =