Node.js 我想向elemmatch查询
例如,我的猫鼬模式:Node.js 我想向elemmatch查询,node.js,mongodb,Node.js,Mongodb,例如,我的猫鼬模式: const newwant = new mongoose.Schema({ statuscol : { type : Boolean, default : false, required :true } }) const Rooms = new mongoose.Schema({ createdBy : { type : String, required : true }, Roomname : {
const newwant = new mongoose.Schema({
statuscol : {
type : Boolean,
default : false,
required :true
}
})
const Rooms = new mongoose.Schema({
createdBy : {
type : String,
required : true
},
Roomname : {
type : String,
required : true
},
createdDate : {
type : Date,
default : Date.now
},
Message : [{
username : String,
date : Date
}],
wants : [newwant]
})
将添加我用express编写的api,并且可以将false值返回true。我
现在要做的是只冻结其值在wants数组中的对象
我为此尝试了这样的代码,但没有成功
router.get("/accepted",async(req,res) => {
try{
rooms.findById(req.body.id,{wants : {$elemMatch : {"statuscol" : true}}},
function(err, result) {
if (err) {
res.send(err);
} else {
res.json(result);
}
}
)
}catch(err){
console.log(err)
}
})
当我运行这段代码时,尽管集合中有2个true,但它只冻结了一个对象,因此它对我不起作用,如果您能提供帮助,我将非常高兴,如果您有任何问题,可以询问
"mongoose": "^5.12.2"
findById()
触发findOne()
中间件。因此,使用find({u-id:req.body.id})
您正在使用findById
它将只获取一个\u-id
与req.body.id
匹配的文档。我想要的是查询我用id找到的集合中的对象数组。