[英]mongodb mongoose find - how do I get applicable strings as result?
[英]how to get the specific result according to a certain condition in MongoDb through mongoose
我想通過貓鼬獲取特定用戶的文章,該怎么辦? 作為我的代碼:
Content.find().sort({_id:-1}).populate(['category','user']).then(function (content) {
res.render('admin/content_list',{
pid:req_pid,
title:'博客管理后台',
userInfo:req.userInfo,
contents: content
})
})
我通過這篇文章獲得了所有文章,但是我想要的是屬於特定用戶的結果,我該怎么辦?
你可以做:
Content.find({userid:USERID}).sort({_id:-1}).populate(['category','user']).then(function (content) {
res.render('admin/content_list',{
pid:req_pid,
title:'博客管理后台',
userInfo:req.userInfo,
contents: content
})
})
其中USERID是用戶的ObjectId。
假設您正在使用貓鼬,您可能還想看看以下內容: http ://mongoosejs.com/docs/queries.html。
您可以使用特定用戶的objectId過濾結果。 只要您具有該ID,就可以執行以下操作:
const mongoose = require('mongoose');
const ObjectId = require('mongoose').Types.ObjectId;
Content.find({
user: new ObjectId(userId)
})
.sort({_id: -1})
.populate(['category', 'user'])
.exec((err, results) => {
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.