Javascript Mongoose-如何确定查询中是否已经存在嵌套对象?
我的数据库存储一个Javascript Mongoose-如何确定查询中是否已经存在嵌套对象?,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,我的数据库存储一个用户,该用户有一个watchMovies[]数组,其中包含一个嵌套对象movie 当接收到包含watchedMovie对象的更新/放置API请求时,我想检查id=zzz的movie是否已经存在于watchedMovie[]数组中 问题是,这个查询不能做到这一点。我做错了什么 User.find( { username: req.params.username, 'watchedMovies.movie.id': movie.id }, function (err, co
用户
,该用户有一个watchMovies[]
数组,其中包含一个嵌套对象movie
当接收到包含watchedMovie
对象的更新/放置API请求时,我想检查id=zzz的movie
是否已经存在于watchedMovie[]
数组中
问题是,这个查询不能做到这一点。我做错了什么
User.find(
{ username: req.params.username, 'watchedMovies.movie.id': movie.id },
function (err, count) {
if (!err) exist = true;
}
);
完整代码如下:
模式
var userSchema = new Schema({
username: { type: String, require: true },
firstName: String,
lastName: String,
...
watchedMovies : [{
movie: { type: Schema.Types.ObjectId, ref: 'Movie' },
time: Date,
watchedDuration: Number,
}],
})
JSON请求正文
{
"id": "10-things-i-hate-about-you", //movie id
"time": "2012-05-29T17:57:30.169Z",
"watchedDuration": "88"
}
router.put('/:username/watchedMovies', function (req, res, next) {
//find movie
Movie.findOne({ id: req.body.id }, function (err, movie) {
if (err) res.send(err);
//find if user already has the movie in the watchedMovies list
var exist = false;
User.find(
{ username: req.params.username, 'watchedMovies.movie.id': movie.id },
function (err, count) {
if (!err) exist = true;
}
);
//if movie is already in watchedList, update User with it
if (exist) {
//set existing watchedMovie object
User.update(...$set...)
} else { // push new
User.findOneAndUpdate(...$push...)
}
});
});
猫鼬查询
{
"id": "10-things-i-hate-about-you", //movie id
"time": "2012-05-29T17:57:30.169Z",
"watchedDuration": "88"
}
router.put('/:username/watchedMovies', function (req, res, next) {
//find movie
Movie.findOne({ id: req.body.id }, function (err, movie) {
if (err) res.send(err);
//find if user already has the movie in the watchedMovies list
var exist = false;
User.find(
{ username: req.params.username, 'watchedMovies.movie.id': movie.id },
function (err, count) {
if (!err) exist = true;
}
);
//if movie is already in watchedList, update User with it
if (exist) {
//set existing watchedMovie object
User.update(...$set...)
} else { // push new
User.findOneAndUpdate(...$push...)
}
});
});
我认为这是个问题,这是你的模型
var userSchema = new Schema({
username: { type: String, require: true },
firstName: String,
lastName: String,
...
watchedMovies : [{
movie: { type: Schema.Types.ObjectId, ref: 'Movie' },
time: Date,
watchedDuration: Number,
}],
})
但你的问题是
{ username: req.params.username, 'watchedMovies.movie.id': movie.id }
不要使用电影id
试试这个
{ username: req.params.username, 'watchedMovies.movie': movie.id }
我认为这是个问题,这是你的模型
var userSchema = new Schema({
username: { type: String, require: true },
firstName: String,
lastName: String,
...
watchedMovies : [{
movie: { type: Schema.Types.ObjectId, ref: 'Movie' },
time: Date,
watchedDuration: Number,
}],
})
但你的问题是
{ username: req.params.username, 'watchedMovies.movie.id': movie.id }
不要使用电影id
试试这个
{ username: req.params.username, 'watchedMovies.movie': movie.id }
是的,这很有效!withmovie.\u id
with是参考id。是的,这有效!withmovie.\u id
with是参考id。