首页 新闻 会员 周边

mongo exists 查询用法:内嵌文档是数组时怎样查询?

0
悬赏园豆:20 [已解决问题] 解决于 2017-04-26 15:56

mongo中的所有文档都是如下的格式,我如果想查询items中某个值是否存在,要怎样写查询语句?

"items" : [

  {

    "subject" : "yuwen",

    "time" : "上午",

    "teacher" : "A"

  },

  {

    "subject" : "shuxue",

    "time" : "下午",

    "teacher" : "B"

  },

  {

    "subject" : "english",

    "time" : "下午",

    "teacher" : "B"

  },

  {

    "subject" : "dili",

    "time" : "上午",

    "teacher" : "C"

  }

]

比如我想查询存在下面值的所有文档,

  {

    "subject" : "shuxue",

    "time" : "下午",

    "teacher" : "B"

  }

这样写不起作用:

db.test.find({ "$elemMatch":{"items.subject":"shuxue","$exists":true} });

也就是说‘$elemMatch’与‘$exists’组合起来,怎样使用?

营营的主页 营营 | 初学一级 | 园豆:186
提问于:2017-04-25 16:32
< >
分享
最佳答案
0

db.test.find({"items.subject":"shuxue"}) 不可以吗?

收获园豆:20
刀是什么样的刀 | 小虾三级 |园豆:910 | 2017-04-25 20:28

整个mongo中有很多类似这样的文档,我怎样找出不包含这个部分的文档?
{
    "subject" : "shuxue",
    "time" : "下午",
    "teacher" : "B"
  }

营营 | 园豆:186 (初学一级) | 2017-04-26 08:16

@营营: 你的输入输出描述的很不清楚。

“我怎样找出不包含这个部分的文档?”

是想说 subject = "shuxue"  但是 time 不能为下午, teacher 不为 B 吗?

刀是什么样的刀 | 园豆:910 (小虾三级) | 2017-04-26 13:52
其他回答(2)
0

用的$where ,虽然效率低点,得到结果最重要,‘$where’真是mongo查询的终极武器啊
db.childtable.find({ "$where":function(){
for(var current in this){
......
}
}
});

营营 | 园豆:186 (初学一级) | 2017-04-26 15:55
0
RedFox红狐 | 园豆:208 (菜鸟二级) | 2017-05-23 11:28
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册