저는 노드와 몽구스를 사용하는 초보 개발자이며 몽구스로 쿼리를 연결하는 가장 좋은 방법이 무엇인지 궁금합니다. 나는 아래처럼 행동하고있다.몽구스 쿼리를 연결하는 가장 좋은 방법
User.findByIdAndUpdate(req.params._id, user, { upsert: true })
.exec((err, updatedUser) => {
if (addedCollections) {
return User.findByIdAndUpdate(req.params._id, { $push: { _collections: { $each: addedCollections } } }, { upsert: true }).exec();
}
return new Query;
})
.exec(() => {
return User.findById(req.params._id).populate('_collections');
})
.exec((err, user) => {
res.json({ user });
})
어떻게 체인 여러 쿼리를 할 수 있습니까?
User.findByIdAndUpdate(req.params._id, user, { upsert: true })
.then(updatedUser => {
if (addedCollections) {
return User.findByIdAndUpdate(req.params._id, { $push: { _collections: { $each: addedCollections } } }, { upsert: true });
}
})
.then(() => {
return User.findById(req.params._id).populate('_collections');
})
.then(user => {
res.json({ user });
})
.catch(err => {
res.status(500).json({ error : err });
});