duanji9378 2013-05-13 08:01
浏览 47
已采纳

在mongodb中使用AND在同一个键上

I have a key in my document whose structure is as follow:

"tag": [
    {
      "schemeName": "http:\/\/somesite.com\/categoryscheme2",
      "name": "Test Tag2",
      "value": 1,
      "slug": "test_tag2"
    },
    {
      "schemaName": "http:\/\/somesite.com\/categoryscheme3",
      "name": "Test Tag3",
      "value": 1,
      "slug": "test_tag3"
    }
  ]

Now, I get inputs as tag=test_tag2ANDtest_tag3. How can I write a query for this? I tried to iterate through the loop but I didnt got any results.

  • 写回答

2条回答 默认 最新

  • duanjitong7226 2013-05-13 09:57
    关注

    Correct me if I am wrong but you don't need an $and or $elemMatch, instead:

    $mongodb->collection->find(array('tags.slug'=>array(
        '$in' => array('test_tag2','test_tag3'))))
    

    Should work, however, if your English suggests what a second read does, then you can also use $all in place of $in. This will ensure that all root documents must have those slugs in them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退