Mongoose Model.find() 参数问题
Mongoose Model.find() parameter issue
我现在正在开发一个节点应用程序(带有 IOS 前端)并且偶然发现了这个问题。我将 mongodb 与 mongoose 一起使用。我有这条路线,/get 接收正确的用户 ID 并尝试查找具有相同用户 ID 的所有 'Voots'。这是 'Voot' 的样子:
{
"_id": "59db9fa2659bb30004899f05",
"title": "Pizza!",
"body": "hoppakeeee",
"user": {
"__v": 0,
"password": "a$Rwb5n7QoKaFaAOW37V0aWeEeYgfn6Uql474ynUXb83lHi7H2CuB1u",
"email": "noelle.schrikker@planet.nl",
"name": "Noelle Schrikker",
"_id": "59db9ecf659bb30004899f04"
},
"__v": 0,
"downVotes": [],
"upVotes": []
},
如您所见,它有一个名为 user
的 属性,它是一个包含姓名、电子邮件、密码和 _id
.
的用户对象
我按照我的要求这样做:
// Send all voots from the specific user
Voot.find({"user._id": userId}, function(err, voots) {
if (err) {
console.log(err);
res.status(400).send(err)
res.end()
}
if (voots) {
res.status(200).send(voots)
res.end()
}
})
我试图找出所有 user
的 属性 为 userId
的投票(这是正确的用户 ID)。但是,这不起作用。我尝试通过 "user.email"
找到它,但确实有效。我觉得跟id
之前的_
有关系。不胜感激!
Voot shema:
var vootSchema = new mongoose.Schema({
title: String,
body: String,
user: {
type: mongoose.Schema.Types,
ref: 'user'
},
upVotes: [String],
downVotes: [String]
})
var Voot = mongoose.model('voot', vootSchema)
用户架构:
var userSchema = new mongoose.Schema({
name: String,
email: String,
password: String
})
var User = mongoose.model('user', userSchema)
我假设 user
对象的 _id
不是字符串。那就是说您需要修改查询以使用 ObjectId
而不是 string:
Voot.find({"user._id": ObjectId(userId)}, function(err, voots) {
if (err) {
console.log(err);
res.status(400).send(err)
res.end()
}
if (voots) {
res.status(200).send(voots)
res.end()
}
})
如果您不想更改您的查询,您可以更改您的 user
架构,以便 _id
是字符串。然后您的查询应该开始工作:
var userSchema = new mongoose.Schema({
_id: { type: String },
name: String,
email: String,
password: String
})
var User = mongoose.model('user', userSchema)
在查询中仅使用 user 而不是 user._id:
Voot.find({ "user": userId }, function(err, voots) {
// Callback code
})
id 或引用的用户存储在 user 字段中。用户子文档以及 user._id 字段仅在填充后可用。
知道了!我在 Voot 模式中添加了 .ObjectId
,现在我可以使用人口访问用户对象。我现在可以使用以下方法找到 Voot:
Voot.find({“user”: userId}).populate(‘user’) .exec()
感谢大家的回答!
我现在正在开发一个节点应用程序(带有 IOS 前端)并且偶然发现了这个问题。我将 mongodb 与 mongoose 一起使用。我有这条路线,/get 接收正确的用户 ID 并尝试查找具有相同用户 ID 的所有 'Voots'。这是 'Voot' 的样子:
{
"_id": "59db9fa2659bb30004899f05",
"title": "Pizza!",
"body": "hoppakeeee",
"user": {
"__v": 0,
"password": "a$Rwb5n7QoKaFaAOW37V0aWeEeYgfn6Uql474ynUXb83lHi7H2CuB1u",
"email": "noelle.schrikker@planet.nl",
"name": "Noelle Schrikker",
"_id": "59db9ecf659bb30004899f04"
},
"__v": 0,
"downVotes": [],
"upVotes": []
},
如您所见,它有一个名为 user
的 属性,它是一个包含姓名、电子邮件、密码和 _id
.
我按照我的要求这样做:
// Send all voots from the specific user
Voot.find({"user._id": userId}, function(err, voots) {
if (err) {
console.log(err);
res.status(400).send(err)
res.end()
}
if (voots) {
res.status(200).send(voots)
res.end()
}
})
我试图找出所有 user
的 属性 为 userId
的投票(这是正确的用户 ID)。但是,这不起作用。我尝试通过 "user.email"
找到它,但确实有效。我觉得跟id
之前的_
有关系。不胜感激!
Voot shema:
var vootSchema = new mongoose.Schema({
title: String,
body: String,
user: {
type: mongoose.Schema.Types,
ref: 'user'
},
upVotes: [String],
downVotes: [String]
})
var Voot = mongoose.model('voot', vootSchema)
用户架构:
var userSchema = new mongoose.Schema({
name: String,
email: String,
password: String
})
var User = mongoose.model('user', userSchema)
我假设 user
对象的 _id
不是字符串。那就是说您需要修改查询以使用 ObjectId
而不是 string:
Voot.find({"user._id": ObjectId(userId)}, function(err, voots) {
if (err) {
console.log(err);
res.status(400).send(err)
res.end()
}
if (voots) {
res.status(200).send(voots)
res.end()
}
})
如果您不想更改您的查询,您可以更改您的 user
架构,以便 _id
是字符串。然后您的查询应该开始工作:
var userSchema = new mongoose.Schema({
_id: { type: String },
name: String,
email: String,
password: String
})
var User = mongoose.model('user', userSchema)
在查询中仅使用 user 而不是 user._id:
Voot.find({ "user": userId }, function(err, voots) {
// Callback code
})
id 或引用的用户存储在 user 字段中。用户子文档以及 user._id 字段仅在填充后可用。
知道了!我在 Voot 模式中添加了 .ObjectId
,现在我可以使用人口访问用户对象。我现在可以使用以下方法找到 Voot:
Voot.find({“user”: userId}).populate(‘user’) .exec()
感谢大家的回答!