gpt4 book ai didi

javascript - 如何在 mongodb 中使用 $nin 运算符从集合中获取文档

转载 作者:行者123 更新时间:2023-12-03 15:59:08 25 4
gpt4 key购买 nike

我有一个像这样的用户架构

const userSchema= new mongoose.Schema({

Username:{
type:String,
trim:true,
required:true
},
email:{
type:String,
trim:true,
required:true
},
hashed_password:{
type:String,
trim:true,
required:true
},
salt:String,
created:{
type:Date,
default:Date.now,
},
updated:Date,
photo:{
data:Buffer,
contentType:String
},

about:{
type:String
},

following:[{type:ObjectId,ref:"User",}],

followers:[{type:ObjectId,ref:"User"}]
})

当我运行查询时

User.find({$_id:{$nin:following}} , (err, users) => {
if (err) {
return res.status(400).json({
error: err,
});
}
res.json(users)
// let data=users

// console.log(following.length,"following")

// console.log(newuser)

}).select("Username")


};
the following variable is an array of objects like this

[{"_id":"5e9c2c7aa9f4bc46e88c7635","用户名":" bat 侠"},{"正​​在关注":[],"关注者":[],"_id":"5ead159d3b41d7061ce639fe","已创建":"2020-05-13T19:37:17.161Z"}]

after running the query i get an error:
"errmsg": "unknown top level operator: $_id",
"code": 2,
"codeName": "BadValue",

如何在查询中正确输入字段

最佳答案

_id中删除$,就像运算符mongodb一样,将其识别为运算符,例如($match等) ,但是没有这样的运算符,所以它会抛出错误

User.find({_id:{$nin:following}} , (err, users) => {
if (err) {
return res.status(400).json({
error: err,
});
}
res.json(users)
// let data=users

// console.log(following.length,"following")

// console.log(newuser)

}).select("Username")


};

关于javascript - 如何在 mongodb 中使用 $nin 运算符从集合中获取文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61783578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com