gpt4 book ai didi

node.js - 如何根据mongodb中的对象属性删除数组中的对象?

转载 作者:太空宇宙 更新时间:2023-11-04 03:11:47 25 4
gpt4 key购买 nike

这是我的 mongodb 文档。

"_id" : ObjectId("5123731f2763c9682a000001"),
"created_on" : ISODate("2013-02-19T12:42:07.835Z"),
"currentLogin" : ISODate("2013-02-22T06:03:25.603Z"),
"email" : "xxx@test.com",
"extraPhotos" : 0,
"reqmail" : [ ],
"isActivated" : true,
"lastLogin" : ISODate("2013-02-20T05:42:15.359Z"),
"linkedAccounts" : [
{
"birthday" : "11/01/1984",
"email" : "testingxxx234@gmail.com",
"expiresIn" : "5184000",
"expiryMailNotifFlag" : -1,
"extraPhotos" : 0,
"fetchFromDate" : null,
"fetchOrder" : "oldest",
"fetchUntilDate" : "04/11/2014",
"locale" : "en_US",
"optIn" : true,
"timezone" : "5.5",
"userID" : "100000207309657"
}
],
"maxLinkedAccounts" : 4,
"name" : "venu wale",
"notifications" : [
{
"_id" : ObjectId("51275fb7389b85f222000001"),
"type" : "limitUsed",
"message" : "Account limit is used",
"accID" : "100000207309657",
"date" : ISODate("2013-02-22T12:08:23.419Z")
},
{
"_id" : ObjectId("51275fe8389b85f222000002"),
"type" : "limitUsed",
"message" : "Account limit is used",
"accID" : "100000207309657",
"date" : ISODate("2013-02-22T12:09:12.385Z")
},
{
"_id" : ObjectId("51276020389b85f222000003"),
"type" : "limitUsed",
"message" : "Account limit is used",
"accID" : "100000207309657",
"date" : ISODate("2013-02-22T12:10:08.275Z")
},
{
"_id" : ObjectId("5127605d389b85f222000004"),
"type" : "limitUsed",
"message" : "Account limit is used",
"accID" : "100000207309657",
"date" : ISODate("2013-02-22T12:11:09.946Z")
},
{
"_id" : ObjectId("51276097389b85f222000005"),
"type" : "limitUsed",
"message" : "Account limit is used",
"accID" : "100000207309657",
"date" : ISODate("2013-02-22T12:12:07.384Z")
},
{
"_id" : ObjectId("512760d2389b85f222000006"),
"type" : "limitUsed",
"message" : "Account limit is used",
"accID" : "100000207309657",
"date" : ISODate("2013-02-22T12:13:06.933Z")
}
],
"password" : "638cfc167e8431c01227e4f113ec9427821b12493ed6fd3",
"phone" : "5555555555",
"photo_updated_time" : 1361341819,
"photosProcessed" : 15,
"photosToProcess" : 400,
"plan" : {
"type" : "photoOnly"
},
"salt" : "af8ab2f5866642f03d31aa3f4a24e25e287bedc7",
"updated_on" : ISODate("2013-02-19T12:42:07.835Z")

我想从通知数组中删除任何一个对象。我正在使用这个查询。

db.users.update({phone:'5555555555'},{$unset:{notifications:{$elemMatch:{_id:ObjectId('51276020389b85f222000003')}}}})

但它不起作用。

最佳答案

您只需pull notifications 数组中的此元素:

db.collection.update({phone:"5555555555"},{$pull:{notifications: {_id:ObjectId("51276020389b85f222000003")}}})

使用 pull,您可以删除与内部查询匹配的所有元素。下一个$pull

{$pull:{ notifications: {type: "limitUsed"}}}

将删除所有“limitUsed”类型的通知。

关于node.js - 如何根据mongodb中的对象属性删除数组中的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15024135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com