Mongoose删除嵌套文档无法正常工作

时间:2015-03-10 11:56:18

标签: mongoose

我的问题是:我没有找到任何问题的解决方案。我的问题是:我有一个这样的嵌套文档:

Post对象是主要对象,Post对象有两个嵌入对象Like和Comment。这是javascript模式代码。

var PostSchema = new Schema({
             username: String,
             post: String,
             date: {
                   type: Date,
                   default: Date.now
                   },
        likes:[Likes.Schema],
        comments : [Comments.Schema]
});



var LikeSchema = new Schema({
                   username: String,
                   date: {
                         type: Date,
                         default: Date.now
                   }
});

var CommentSchema = new Schema({
            username: String,
            comment: String,
            date: {
                  type: Date,
                  default: Date.now
                  }
});

我的测试数据在mongodb上是这样的:

{
"_id": {"$oid": "54fdbedf7c3da52a065ba71b"},
"post": "Deneme postuı",
"username": "fatihayyildiz",
"comments": [],
"likes": {
    "username": "gurkan",
    "_id": {"$oid": "54fdbf067c3da52a065ba71c"},
    "date": {"$date": "2015-03-09T15:40:54.368Z"}
},
"date": {"$date": "2015-03-09T15:40:15.100Z"},
"__v": 0
}

所以如果我回到我的问题,我想从我的帖子对象中删除这个对象。

{
    "username": "gurkan",
    "_id": {"$oid": "54fdbf067c3da52a065ba71c"},
    "date": {"$date": "2015-03-09T15:40:54.368Z"}
}

所以我尝试了这个解决方案。如你所知,我使用mongoose库进行数据库操作:

    var query = Like.find({ '_id': likeid });
    //console.log(query);
    query.remove(function(){

        //console.log("Deleted");
        res.json({
                status:"Deleted"
        });

    }).exec();

这段代码推送给我删除但没有错误,但是当我检查我的数据库时,它仍然存在。

Mongoose版本是" ^ 3.8.24"

如何从Post对象中删除此嵌套对象?

1 个答案:

答案 0 :(得分:0)

您需要了解一些事情。首先,如果您与Post对象一起创建Like对象,它将只存在于那里。例如:

# js
new Post({
    username: 'test',
    likes: [
        {
            username: test,
        },
    ],
}).save(function(err, post) {
    ...
});

# mongo
db.posts.find()
{ "username" : "test", "_id" : ObjectId("54ff6091933a118f790180e4"), "likes" : [  {  "_id" : ObjectId("54ff6091933a118f790180e3"),  "username" : "test" } ] }
db.likes.find()

要删除相似的对象,您需要从帖子中拉出对象,如下所示:

# js
post.likes.pull({_id: '54ff6091933a118f790180e3'});
post.save();

# mongo
db.posts.find()
{ "username" : "test", "_id" : ObjectId("54ff6091933a118f790180e4"), "likes" : [ ] }

如果你想让Like成为他们自己的对象,你需要先创建对象并将其添加到帖子中:

# js
new Like({
    username: 'test',
}).save(function(err, like) {
    new Post({
        username: 'test',
        likes: [like],
    }).save(function(err, post) {
        ...
    });  
})

# mongo
db.posts.find()
{ "username" : "test", "_id" : ObjectId("54ff6091933a118f790180e4"), "likes" : [  {  "_id" : ObjectId("54ff6091933a118f790180e3"),  "username" : "test" } ] }
db.likes.find()
{ "username" : "test", "_id" : ObjectId("54ff6091933a118f790180e3") }

但这并不意味着您可以简单地删除Like对象,它将从帖子中删除。你仍然需要拉它。

# js
like.remove();

# mongo
db.posts.find()
{ "username" : "test", "_id" : ObjectId("54ff6091933a118f790180e4"), "likes" : [  {  "_id" : ObjectId("54ff6091933a118f790180e3"),  "username" : "test" } ] }
db.likes.find()
希望解释一下。为了让它无缝地工作,你应该考虑在你的Like对象上删除钩子,这样每当一个对象被删除时它将从Post中删除它。

LikeSchema.post('remove', function(like) {
    Post.find({likes: like._id}, function(err, posts) {
        for (var post in posts) {
            post.likes.pull({_id: like._id}).save();
        }
    })
})

或类似的东西。