如何从猫鼬的数组中删除带有嵌套用户的对象

时间:2018-07-24 14:40:45

标签: mongodb mongoose

我正在使用猫鼬,并尝试从Friendships_requested_to_user数组中删除一个用户,并将其推入friends数组。这是用户架构:

var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");

var UserSchema = new mongoose.Schema({
    friends: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "friendship_requested_by_user"
        }
    ],
    friendships_requested_by_user: [
        {
            user: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "friendship_requested_by_user"
            },
            level: Number
        }
    ],
    friendships_requested_to_user: [
        {
            user: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "friendship_requested_by_user"
            },
            level: Number
        }
    ],
});

UserSchema.plugin(passportLocalMongoose);

module.exports = mongoose.model("User", UserSchema);

这是我以前分别将它们添加到数组friendships_requested_to_userfriendships_requested_by_user中的方法(起作用):

founduser.friendships_requested_to_user.push({user: friend_to_be, level: req.body.friendship_level});
friend_to_be.friendships_requested_by_user.push({user: friend_to_be, level: req.body.friendship_level});

这是我用来从数组中删除它们的方法(不起作用):

founduser.friendships_requested_to_user.pull({ user: {id: friend_to_be._id}});
friend_to_be.friendships_requested_by_user.pull({ user: {id: founduser._id}});

我在做什么错了?

注意:founduser和friend_to_be都是唯一用户。

0 个答案:

没有答案