dongnuan7956 2018-11-28 11:55
浏览 447

mongodb查询一个数组中一个数组中的一个数组

I have a collection of users. Each of those have an array of bookmarks. Each bookmark has an array of categories it belongs to. Leading to a structure like this:

[
    {name: "Bob",
     bookmarks: [
         {url: "http://duckduckgo.com",
          categories: [
              "Search",
              "Ducks",
          ],
         },
     ],
    },
]

Now given a name and a url and a category name. I want to delete said category of the respective bookmark. But my problem is that all attempts return the whole user or delete the whole bookmark and not just the category.

This is my best attempt using the mgo driver so far:

type arbitraryJson map[string]interface{}
user := "Bob"
bookmarkURL := url.Parse("http://duckduckgo.com")
tagName := "Search"

err = userDB.Update(
    arbitraryJson{
        "name":          user,
        "bookmarks.url": bookmarkURL.String(),
    },
    arbitraryJson{
        "$pull": arbitraryJson{
            "bookmarks.categories": tagName,
        },
    },
)

Which translates (I think) to the mongo query:

db.users.updateOne(
   { name: "Bob",
     bookmarks.url: "http://duckduckgo.com" }
   {
     $pull: { bookmarks.categories: "search" }
   }
)
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记