gpt4 book ai didi

spring - MongoDB $pull 数组 2 级

转载 作者:可可西里 更新时间:2023-11-01 10:43:54 38 4
gpt4 key购买 nike

我正在尝试从具有 e 两级复杂度的数组中提取一个元素

我的文档:

> db.users.find({ mail : 'titi@toto.fr'}).pretty()
{
"_class" : "bean.User",
"_id" : ObjectId("52f504bb2f9dd91186211537"),
"commandes" : [
{
"adresse" : "15 rue de la soif",
"codePostal" : "29200",
"idCommande" : 1,
"montantTotal" : 0,
"nom" : "TOTO",
"prenom" : "tata",
"ville" : "Brest",
"voyagesSouscrits" : [
{
"idVoyage" : "123",
"duree" : 1,
"nbPersonnes" : 0,
"villeDepart" : "Nantes",
"prixVoyage" : 999999
},
{
"idVoyage" : "addVoyageSouscrit",
"duree" : 1,
"nbPersonnes" : 0,
"villeDepart" : "Toulouse",
"prixVoyage" : 7777
}
]
},
{
"idCommande" : 1,
"dateCommande" : ISODate("2014-02-07T16:07:23.930Z"
),
"nom" : "TOTO",
"prenom" : "tata",
"adresse" : "15 rue de la soif",
"ville" : "Brest",
"codePostal" : "29200",
"montantTotal" : 0,
"voyagesSouscrits" : [
{
"idVoyage" : "123",
"duree" : 1,
"nbPersonnes" : 0,
"villeDepart" : "Toulouse",
"prixVoyage" : 666666
}
]
}
],
"mail" : "titi@toto.fr",
"password" : "tata"
}

第一步,我想提取 ID 为“123”的“voyagesSoucrits”元素。

根据这篇文章: MongoDB pull element from array two levels deep

我试过了:

> db.users.update({ mail : 'titi@toto.fr', "commandes.voyagesSouscrits.idVoyage" : "123"},{$pull : {"commandes.$.voyagesSouscrits" : {"commandes.voyagesSouscrits.idVoyage" : "123"}}})

没用!

我做错了什么,但我找不到它....有什么想法吗?

最佳答案

您不需要完整的符号,因为占位符已经移动到数组中的那个位置。

db.junk.update(
{ "commandes.voyagesSouscrits.idVoyage": "123" },
{$pull: { "commandes.$.voyagesSouscrits": { idVoyage: "123" } }}
)

这部分:

idVoyage: { <query> }

之所以需要,是因为“commandes.$.voyagesSouscrits”中的位置运算符只能匹配查询中找到的第一个 数组位置。

http://docs.mongodb.org/manual/reference/operator/projection/positional/

希望一切都过去了。

关于spring - MongoDB $pull 数组 2 级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21637772/

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