使用Mongo查询数组中的对象中的字段? [英] Query for a field in an object in array with Mongo?

查看:52
本文介绍了使用Mongo查询数组中的对象中的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用Mongo查询数组对象中字段中具有特定值的条目.

Is it possible to use Mongo to query for entries that have a particular value in a field in an object in an array.

例如,假设我要查找field1具有对象数组的所有对象,其中一个对象具有值为1的字段"one".此查询应从我的集合中返回以下对象:

For example, let's say I want to find all objects where field1 has an array of objects, one of which has the field 'one' with a value of 1. This query should return the following object from my collection:

{_id: 0000, field1: [{one: 1, two: 2}, {one: 'uno', two: 'dos'}]}

推荐答案

我想您需要的是:

db.collection.find( { field1: { $elemMatch: { one: 1 } } } );

http://docs.mongodb.org/manual/reference /operator/elemMatch/#op._S_elemMatch

这篇关于使用Mongo查询数组中的对象中的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆