【问题标题】:Mongodb arrays - $pull from embedded documentMongodb 数组 - 从嵌入式文档中提取
【发布时间】:2021-04-06 11:10:07
【问题描述】:
{
    "_id" : ObjectId("60605d10f9c97907028172f5"),
    "dbname" : "123456789-123456789",
    "logindetails" : [
        {
            "email" : "ankit@gmail.com",
            "password" : "$2a$04$i7AGR.zG113mjIshA.Me7O/Wv9zIcuBy58GOqb2Gf76Xk5WgYxGIu",
            "sessionid" : [
                "bca0deeb-66ed-4f1e-8d76-7ea2f04102b1"
            ],
            "name" : "Ankit",
            "initial" : "ab",
            "updateToken" : ""
        }
    ]
}

在我的 Mongodb 集合 userDetails 中,我有上面的文档,我想删除 sessionid 值。我正在使用以下查询:

db.userDetails.updateOne({"dbname":"123456789-123456789"},{"$pull":{"logindetails.sessionid":"bca0deeb-66ed-4f1e-8d76-7ea2f04102b1"}})

我收到以下错误:

"errmsg" : "Cannot use the part (sessionid) of (logindetails.sessionid) to traverse the element ({logindetails: [ { email: \"ankit.s.bagaria@gmail.com\", password: \"$2a$04$i7AGR.zG113mjIshA.Me7O/Wv9zIcuBy58GOqb2Gf76Xk5WgYxGIu\", sessionid: [ \"bca0deeb-66ed-4f1e-8d76-7ea2f04102b1\" ], name: \"Ankit Bagaria\", initial: \"ab\", updateToken: \"\" } ]})"

什么是正确的查询?

【问题讨论】:

    标签: arrays mongodb mongodb-query


    【解决方案1】:

    演示 - https://mongoplayground.net/p/LgYjRIUzSlJ

    使用https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/

    过滤的位置运算符 $[] 识别与更新操作的 arrayFilters 条件匹配的数组元素

    与 arrayFilters 选项结合使用,$[] 运算符具有以下形式:

    db.collection.update(
    {"dbname": "123456789-123456789" },
    { "$pull": { "logindetails.$[l].sessionid": "bca0deeb-66ed-4f1e-8d76-7ea2f04102b1" } },
    { arrayFilters: [ { "l.sessionid": "bca0deeb-66ed-4f1e-8d76-7ea2f04102b1" } ]}
    )
    

    【讨论】:

      猜你喜欢
      • 2020-03-23
      • 2013-03-20
      • 2017-01-21
      • 2021-08-07
      • 1970-01-01
      • 2015-02-18
      • 1970-01-01
      • 2021-03-24
      • 2018-06-03
      相关资源
      最近更新 更多