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 彩灯控制电路,会的加我QQ1482956179
    • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
    • ¥15 (关键词-电路设计)
    • ¥15 如何解决MIPS计算是否溢出
    • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
    • ¥15 操作系统相关算法中while();的含义
    • ¥15 CNVcaller安装后无法找到文件
    • ¥15 visual studio2022中文乱码无法解决
    • ¥15 关于华为5g模块mh5000-31接线问题
    • ¥15 keil L6007U报错