-1

Example document:

{
    "aliases" : {
        "name" : [ 
            "Brinton McKay", 
            "Dr. Theopolis", 
            "Galactic Spiral Sound", 
            "Highrise", 
            "Memory Boy", 
            "Semblance Factor", 
            "Spy (2)", 
            "Three O'Clock High"]}
}

How i can count the number of "name" under "aliases"? I would like to print all _id who which contains more than 3 aliases. any help would be appreciated.

2
  • 1
    Take a look at this answer. Commented Jun 10, 2015 at 22:07
  • So in this case it would be: db.coll.find({'aliases.name.3': {$exists: true}}) Commented Jun 11, 2015 at 12:30

1 Answer 1

0

Please check the below query :

db.collection.find({ $where : function() 
{ return Object.keys(this.aliases.name).length > 3 } });

OR

db.collection.aggregate([
{$project : { _id :1 , numb : {$size : "$aliases.name"} }
},
{$match : { numb :{$gt : 3 }}
}
]);

PS : you can see the documentation in below link : http://docs.mongodb.org/manual/reference/operator/query/where/

Sign up to request clarification or add additional context in comments.

5 Comments

This is the error: error: { "$err" : "Can't canonicalize query: BadValue $size needs a number", "code" : 17287 }
can you check the above query ?
It works but as { $size: 3 } not as {$size:{$gt:3}} , how i can use $gt? Thanks very much
I have posted anothet query using aggregate function, can you check that as well. I have edited that in my post. please try them.
This one is perfect: db.coll.find({'aliases.name.3': {$exists: true}}) Great JohnnyHK & also Yathish. You guys are good.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.