Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mongodb filter array

/**
Assume we have docs in MyCollection with the folowing structure:
{
	"users": [
    	{"name": "John", "age": 34},
        {"name": "Kate", "age": 17},
        {"name": "Simon", "age": 65},
        {"name": "Alice", "age": 27}
    ]
}
*/

db.MyCollection.aggregate([
  {
    "$project": {
      "users": {
        "$filter": {
          "input": "$users",
          "as": "user",
          "cond": {
            "$and": [
              {"$gte": ["$$user.age", 18]},
              {"$lte": ["$$user.age", 50]}
            ]
          }
        }
      }
  	}
  }
])

/**
This way we'll have the result
{
    "_id" : ObjectId("63690f75ccdacb91c24682f0"),
    "users" : [ 
        {
            "name" : "John",
            "age" : 34
        }, 
        {
            "name" : "Alice",
            "age" : 27
        }
    ]
}
*/
Comment

PREVIOUS NEXT
Code Example
Javascript :: number pyramid javascript 
Javascript :: get current time in javascript 
Javascript :: postgresql update json field key value 
Javascript :: eslint allow console 
Javascript :: Using webpack 5. Reason: future.webpack5 option enabled https://nextjs.org/docs/messages/webpack5 
Javascript :: How to add and play sounds in JS 
Javascript :: Custom jquery validation messages 
Javascript :: onload multiple functions 
Javascript :: document.write multiple lines 
Javascript :: useHistory react-router-dom 
Javascript :: hide label chratjs 
Javascript :: how to delete a folder in node js 
Javascript :: FileReader get filename 
Javascript :: how to create react native project at specific version 
Javascript :: apa itu this pada javascript 
Javascript :: elasticsearch field not exists 
Javascript :: react event target square brackets 
Javascript :: get the value of a checkbox jquery 
Javascript :: isset js 
Javascript :: committing only some changes to git 
Javascript :: clean react app 
Javascript :: js get bytearray from file 
Javascript :: how to wait until a variable is set javascript 
Javascript :: reference error $ is not defined jquery 
Javascript :: can we find lenght of an object 
Javascript :: select html react 
Javascript :: javascript array random selector 
Javascript :: sequelize.org findById 
Javascript :: image preview using js 
Javascript :: js dynamicly add script 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =