Mongoose删除(拉)数组中的文档,不适用于ObjectID

时间:2022-03-21 02:33:13

I have the following mongoose schema:

我有以下mongoose架构:

user = {
    "userId" : "myId",
    "connections":
    [{
        "dateConnectedUnix": 1334567891,
        "isActive": true
    }, {
        "dateConnectedUnix": 1334567893,
        "isActive": false
    }]
}

I would like to delete the second item in the connections array, to get the following:

我想删除connections数组中的第二项,以获得以下内容:

user = {
    "userId" : "myId",
    "connections": 
    [{
        "dateConnectedUnix": 1334567893,
        "isActive": false
    }]
}

The following code does the job as expected:

以下代码按预期执行工作:

userAccounts.update({'connections.isActive': false }, 
                    {$pull: { 'connections.isActive':false }}, 
                    function (err,val) {
                        console.log(val)
                    });

But, I need to delete based on ObjectId. And the following goes does not work:

但是,我需要基于ObjectId删除。并且以下操作不起作用:

userAccounts.update({'connections._id': '1234-someId-6789' }, 
                    {$pull: { 'connections._id': '1234-someId-6789' }}, 
                    function (err,val) {
                        console.log(val)
                    });

Any suggestions? I have been banging my head against the screen (aka Google, *, ...) for hours and have had no luck.

有什么建议么?我一直在屏幕上撞击屏幕(又名谷歌,*,......)几个小时,没有运气。

4 个解决方案

#1


30  

It seems that the above code would not work. It should not even have worked for the first example I gave.

看来上面的代码不起作用。它甚至不应该适用于我给出的第一个例子。

In the end I was supported by this answer here: MongoDB, remove object from array

最后我得到了这个答案的支持:MongoDB,从数组中删除对象

Here is my working code:

这是我的工作代码:

userAccounts.update( 
      { userId: usr.userId },
      { $pull: { connections : { _id : connId } } },
      { safe: true },
      function removeConnectionsCB(err, obj) {
          ...
      });

#2


12  

I have a document like

我有一个像这样的文件

Mongoose删除(拉)数组中的文档,不适用于ObjectID

I have to delete address from address array

我必须从地址数组中删除地址

After searching lots on internet I found the solution

在网上搜索了很多我找到了解决方案

 Customer.findOneAndUpdate(query, {$pull: {address: addressId}}, function(err, data){
        if(err) {
          return res.status(500).json({'error' : 'error in deleting address'});
        }

        res.json(data);

      });

#3


8  

To use update with ObjectId, you should use ObjectId object instead of string representation :

要使用ObjectId进行更新,您应该使用ObjectId对象而不是字符串表示:

var ObjectId = require('mongoose').Types.ObjectId;

userAccounts.update({'connections._id': new ObjectId('1234-someId-6789') }, 
                {$pull: { 'connections._id': new ObjectId('1234-someId-6789') }}, 
                function (err,val) {
                    console.log(val)
                });

#4


1  

mongoose: 4.11.11
What have worked for me is the following syntax:

mongoose:4.11.11对我有用的是以下语法:

const removeTansactionFromUser = (userId, connectionId) => {
    return User.findByIdAndUpdate(userId, { $pull: { "connections": connectionId} }, {'new': true} );
};

Mongoose support id in string format or ObjectId format.
Tip: new ObjectId(stringId) to switch from string to ObjectId

Mongoose支持id为字符串格式或ObjectId格式。提示:要从字符串切换到ObjectId的新ObjectId(stringId)

#1


30  

It seems that the above code would not work. It should not even have worked for the first example I gave.

看来上面的代码不起作用。它甚至不应该适用于我给出的第一个例子。

In the end I was supported by this answer here: MongoDB, remove object from array

最后我得到了这个答案的支持:MongoDB,从数组中删除对象

Here is my working code:

这是我的工作代码:

userAccounts.update( 
      { userId: usr.userId },
      { $pull: { connections : { _id : connId } } },
      { safe: true },
      function removeConnectionsCB(err, obj) {
          ...
      });

#2


12  

I have a document like

我有一个像这样的文件

Mongoose删除(拉)数组中的文档,不适用于ObjectID

I have to delete address from address array

我必须从地址数组中删除地址

After searching lots on internet I found the solution

在网上搜索了很多我找到了解决方案

 Customer.findOneAndUpdate(query, {$pull: {address: addressId}}, function(err, data){
        if(err) {
          return res.status(500).json({'error' : 'error in deleting address'});
        }

        res.json(data);

      });

#3


8  

To use update with ObjectId, you should use ObjectId object instead of string representation :

要使用ObjectId进行更新,您应该使用ObjectId对象而不是字符串表示:

var ObjectId = require('mongoose').Types.ObjectId;

userAccounts.update({'connections._id': new ObjectId('1234-someId-6789') }, 
                {$pull: { 'connections._id': new ObjectId('1234-someId-6789') }}, 
                function (err,val) {
                    console.log(val)
                });

#4


1  

mongoose: 4.11.11
What have worked for me is the following syntax:

mongoose:4.11.11对我有用的是以下语法:

const removeTansactionFromUser = (userId, connectionId) => {
    return User.findByIdAndUpdate(userId, { $pull: { "connections": connectionId} }, {'new': true} );
};

Mongoose support id in string format or ObjectId format.
Tip: new ObjectId(stringId) to switch from string to ObjectId

Mongoose支持id为字符串格式或ObjectId格式。提示:要从字符串切换到ObjectId的新ObjectId(stringId)