最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

通过ID从猫鼬数组中删除对象

网站源码admin20浏览0评论

通过ID从猫鼬数组中删除对象

通过ID从猫鼬数组中删除对象

所以这似乎很简单,但是我似乎无法使它正常工作。我在mongodb中有一个文档,我使用mongoose即时通讯我所要做的就是通过id查找用户,获取文档并从数组中删除一个指定的对象。对象。这是结构:

report:[
   {
     asset_report_id:234,
     name:'somethign,
    },
    {
     asset_report_id:23,
     name:'somethign,
    },
   {
     asset_report_id:111,
     name:'somethign,
    }
]

我尝试过这个:

User.findOne({_id: request.decodedTokenData.userId})
        .exec()
        .then(user=>{
          const result = user.reports.find( ({ asset_report_id }) => asset_report_id === assetID );
          console.log('IN FIND',result);
        })
        .catch(err=>console.log(err))

现在我确实得到了很好的结果,我可以删除,但是没有一种方法可以直接用猫鼬来做吗?更多的纯蒙哥版本:

db.removeObject.update( {'_id':ObjectId("5c6ea036a0c51185aefbd14f")},
 {$pull:{"reports":{"asset_report_id":234}}},false,true);
回答如下:

所以正确的解决方案是:

发布评论

评论列表(0)

  1. 暂无评论