node.js - Mongoose - get length of array in model -


i have mongoose schema:

var postschema = mongoose.schema({      postid: {         type: number,         unique: true     },      upvotes: [         {             type: number,             unique: true         }     ]  }); 

what best query use length of upvotes array? don't believe need use aggregation because want query 1 model, need length of upvotes array given model.

really struggling find info online - search mentions aggregation methodology don't believe need.

also, side note, unique schema property of upvotes array doesn't work, perhaps doing wrong.

find results can include content docs themselves1, while aggregate can project new values derived doc's content (like array's length). that's why need use aggregate this, though you're getting single doc.

post.aggregate([{$match: {postid: 5}}, {$project: {upvotes: {$size: '$upvotes'}}}]) 

1single exception $meta projection operator project $text query result's score.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -