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

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -