mongodb在数组中找到字段
我正在尝试学习mongoDB,我的数据库看起来像这样:
{
"_id" : ObjectId("625c95836be34e3c73c37316"),
"cuisine" : "Chinese",
"grades" : [
{
"date" : ISODate("2014-02-25T00:00:00Z"),
"grade" : "A",
"score" : 112
},
{
"date" : ISODate("2013-08-14T00:00:00Z"),
"grade" : "C",
"score" : 28
},
{
"date" : ISODate("2012-07-18T00:00:00Z"),
"grade" : "A",
"score" : 13
},
{
"date" : ISODate("2012-03-09T00:00:00Z"),
"grade" : "A",
"score" : 13
},
{
"date" : ISODate("2011-10-27T00:00:00Z"),
"grade" : "A",
"score" : 12
},
{
"date" : ISODate("2011-05-19T00:00:00Z"),
"grade" : "A",
"score" : 13
}
],
"name" : "janette"
},
{
"_id" : ObjectId("625c95836be34e3c73c37317"),
"cuisine" : "Chinese",
"grades" : [
{
"date" : ISODate("2014-01-07T00:00:00Z"),
"grade" : "A",
"score" : 17
},
{
"date" : ISODate("2013-05-09T00:00:00Z"),
"grade" : "B",
"score" : 107
}
],
"name" : "Annie'S Kitchen"
}
每个对象都有一个拥有相同字段的数组。如果我尝试:
db.restaurants.find({"grades.score":{ $gt: 80, $lt: 100 }});
它会抓住两者,但不会抓住。 我还希望能够检查:
db.restaurants.find("grades.score":{$gt: 70},"grades.grade":{/^A$/} });
我需要仅在数组上的字段中检查字段。
我不知道如何更好地解释它,对不起,并感谢您的帮助。
I'm trying to learn mongodb, my database looks like this:
{
"_id" : ObjectId("625c95836be34e3c73c37316"),
"cuisine" : "Chinese",
"grades" : [
{
"date" : ISODate("2014-02-25T00:00:00Z"),
"grade" : "A",
"score" : 112
},
{
"date" : ISODate("2013-08-14T00:00:00Z"),
"grade" : "C",
"score" : 28
},
{
"date" : ISODate("2012-07-18T00:00:00Z"),
"grade" : "A",
"score" : 13
},
{
"date" : ISODate("2012-03-09T00:00:00Z"),
"grade" : "A",
"score" : 13
},
{
"date" : ISODate("2011-10-27T00:00:00Z"),
"grade" : "A",
"score" : 12
},
{
"date" : ISODate("2011-05-19T00:00:00Z"),
"grade" : "A",
"score" : 13
}
],
"name" : "janette"
},
{
"_id" : ObjectId("625c95836be34e3c73c37317"),
"cuisine" : "Chinese",
"grades" : [
{
"date" : ISODate("2014-01-07T00:00:00Z"),
"grade" : "A",
"score" : 17
},
{
"date" : ISODate("2013-05-09T00:00:00Z"),
"grade" : "B",
"score" : 107
}
],
"name" : "Annie'S Kitchen"
}
each object has an array that owns the same fields. If I try:
db.restaurants.find({"grades.score":{ $gt: 80, $lt: 100 }});
it will catch both but should catch none.
Also I want to be able to check:
db.restaurants.find("grades.score":{$gt: 70},"grades.grade":{/^A$/} });
I need to check the fields only with the fields in the same position on the array.
I don't know how to explain it better, sorry, and thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
$ MAP
来投影辅助布尔数组,并使用$ allelementTrue
或$ anyelementTrue
将其过滤。这是您的 first 和第二查询。
You can use
$map
to project an auxiliary boolean array and use$allElementsTrue
or$anyElementTrue
to filter it.Here are the Mongo playgrounds for your first and second query.