I'm beginner in MongoDB. I want to get an array size. I used as below:
db.users.aggregate([
{ $match: { id : "test_id" } },
{ $project : {
id : 1, size : { $cond : {
if : {$isArray: "$sentence"},
then :{$size: "$sentence"},
else: 0
}
}
}
}
])
and I checked this
{"id" : "test_id", "size" : 4 }
but I want to use this in a function as below:
function getSentenceNumber(in_id) {
var ret = db.users.aggregate([
{ $match: { id : in_id } },
{ $project : {
id : 1,
size : {$cond : { if : {$isArray: "$sentence"}, then : {$size: "$sentence"}, else: 0 } }
} }
]);
return ret.size;
}
To use db.users.update()
But I got "undefined".
Would someone help me?