Node.js 具有多个参数的mongoose findOne不起作用
我的架构如下所示:Node.js 具有多个参数的mongoose findOne不起作用,node.js,mongoose,Node.js,Mongoose,我的架构如下所示: itemLike.findOne({ 'item_id': itemData.item_id, 'user': userInfo }).then((info) => { if (!info) { let newItem = new itemLike(); newItem.no_of_likes = 1;
itemLike.findOne({ 'item_id': itemData.item_id, 'user': userInfo }).then((info) => {
if (!info) {
let newItem = new itemLike();
newItem.no_of_likes = 1;
newItem.users.push(userInfo);
newItem.save().then((likeInfo) => {
res.json({
status: '200',
message: 'Thanks for appreciating our product',
data: likeInfo
});
}).catch((err) => {
console.log("hh " + err);
res.json({
status: '500',
message: 'Oops,something went wrong'
});
});
} else {
res.json({
status: '200',
message: 'you have already liked the product'
});
}
}).catch((err) => {
console.log("ll " + err);
res.json({
status: '500',
message: 'Oops,something went wrong'
});
});
{
"status": "200",
"message": "Thanks for appreciating our product",
"data": {
"_id": "5990dd8e669a041fec3aecb9",
"users": [
"591325c6b685bc165313a8ff",
"591325c6b685bc165313a8ef"
],
"no_of_likes": 1
}
}
itemLike.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const itemLike = new Schema({
item_id: { type: mongoose.Schema.ObjectId, ref: 'items', index: true },
no_of_likes: { type: Number, default: 0 },
users: [{ type: mongoose.Schema.ObjectId, ref: 'user' }]
}, { versionKey: false });
module.exports = mongoose.model('item_like', itemLike);
我的查询如下:
itemLike.findOne({ 'item_id': itemData.item_id, 'user': userInfo }).then((info) => {
if (!info) {
let newItem = new itemLike();
newItem.no_of_likes = 1;
newItem.users.push(userInfo);
newItem.save().then((likeInfo) => {
res.json({
status: '200',
message: 'Thanks for appreciating our product',
data: likeInfo
});
}).catch((err) => {
console.log("hh " + err);
res.json({
status: '500',
message: 'Oops,something went wrong'
});
});
} else {
res.json({
status: '200',
message: 'you have already liked the product'
});
}
}).catch((err) => {
console.log("ll " + err);
res.json({
status: '500',
message: 'Oops,something went wrong'
});
});
{
"status": "200",
"message": "Thanks for appreciating our product",
"data": {
"_id": "5990dd8e669a041fec3aecb9",
"users": [
"591325c6b685bc165313a8ff",
"591325c6b685bc165313a8ef"
],
"no_of_likes": 1
}
}
这里假设我的回答如下所示:
itemLike.findOne({ 'item_id': itemData.item_id, 'user': userInfo }).then((info) => {
if (!info) {
let newItem = new itemLike();
newItem.no_of_likes = 1;
newItem.users.push(userInfo);
newItem.save().then((likeInfo) => {
res.json({
status: '200',
message: 'Thanks for appreciating our product',
data: likeInfo
});
}).catch((err) => {
console.log("hh " + err);
res.json({
status: '500',
message: 'Oops,something went wrong'
});
});
} else {
res.json({
status: '200',
message: 'you have already liked the product'
});
}
}).catch((err) => {
console.log("ll " + err);
res.json({
status: '500',
message: 'Oops,something went wrong'
});
});
{
"status": "200",
"message": "Thanks for appreciating our product",
"data": {
"_id": "5990dd8e669a041fec3aecb9",
"users": [
"591325c6b685bc165313a8ff",
"591325c6b685bc165313a8ef"
],
"no_of_likes": 1
}
}
现在假设当我使用
项id:5990dd8e669a041fec3aecb9
和用户:591325c6b685bc165313a8ef
进行findOne查询时,它应该给我确切的文档。但在这种情况下,findOne无法找到documet,我认为由于users
字段是一个ref数组,它无法同时查看这两个参数。那么我该如何实现这个功能呢?如果itemData.item\u id
和userinfo
都是id,那么您可以这样做
itemLike.findOne({
_id: itemData.item_id,
users: {
$in: [userInfo]
}
}).then(...).catch(...);
因为用户
是一个数组,所以可以使用
编辑
查询不起作用,因为您正在查找item\u id
,但代码中没有存储item\u id
但是,查看您的响应,您会发现您要查找的两个值存储在
\u id
和用户中。因此,在您的查询中使用\u id
和用户
。应该是用户
,而不是用户
(单个)。是的,我更改了用户
,但问题仍然存在。为此,我更新了我的问题$in,它无法在users
数组中找到userInfo