MongoDB Dot Notation (Reaching into Objects)

转载自 http://www.mongodb.org/display/DOCS/Dot+Notation+(Reaching+into+Objects)


Dot Notation (Reaching into Objects)
Dot Notation vs. Subobjects
Array Element by Position
Matching with $elemMatch
See Also
MongoDB is designed for storing JSON-style objects. The database understands the structure of these objects and can reach into them to evaluate query expressions.

Let's suppose we have some objects of the form:

> db.persons.findOne()
{ name: "Joe", address: { city: "San Francisco", state: "CA" } ,
likes: [ 'scuba', 'math', 'literature' ] }
Querying on a top-level field is straightforward enough using Mongo's JSON-style query objects:

> db.persons.find( { name : "Joe" } )
But what about when we need to reach into embedded objects and arrays? This involves a bit different way of thinking about queries than one would do in a traditional relational DBMS. To reach into embedded objects, we use a "dot notation":

> db.persons.find( { "address.state" : "CA" } )
Reaching into arrays is implicit: if the field being queried is an array, the database automatically assumes the caller intends to look for a value within the array:

> db.persons.find( { likes : "math" } )
We can mix these styles too, as in this more complex example:

> db.blogposts.findOne()
{ title : "My First Post", author: "Jane",
comments : [{ by: "Abe", text: "First" },
{ by : "Ada", text : "Good post" } ]
}
> db.blogposts.find( { "comments.by" : "Ada" } )
We can also create indexes of keys on these fields:

db.persons.ensureIndex( { "address.state" : 1 } );
db.blogposts.ensureIndex( { "comments.by" : 1 } );
Dot Notation vs. Subobjects

Suppose there is an author id, as well as name. To store the author field, we can use an object:

> db.blog.save({ title : "My First Post", author: {name : "Jane", id : 1}})
If we want to find any authors named Jane, we use the notation above:

> db.blog.findOne({"author.name" : "Jane"})
To match only objects with these exact keys and values, we use an object:

db.blog.findOne({"author" : {"name" : "Jane", "id" : 1}})
Note that

db.blog.findOne({"author" : {"name" : "Jane"}})
will not match, as subobjects have to match exactly (it would match an object with one field: {"name" : "Jane"}). Note that the embedded document must also have the same key order, so:

db.blog.findOne({"author" : {"id" : 1, "name" : "Jane"}})
will not match, either. This can make subobject matching unwieldy in languages whose default document representation is unordered.

Array Element by Position

Array elements also may be accessed by specific array position:

// i.e. comments[0].by == "Abe"
> db.blogposts.find( { "comments.0.by" : "Abe" } )
(The above examples use the mongo shell's Javascript syntax. The same operations can be done in any language for which Mongo has a driver available.)

Matching with $elemMatch

Using the $elemMatch query operator (mongod >= 1.3.1), you can match an entire document within an array. This is best illustrated with an example. Suppose you have the following two documents in your collection:

// Document 1
{ "foo" : [
{
"shape" : "square",
"color" : "purple",
"thick" : false
},
{
"shape" : "circle",
"color" : "red",
"thick" : true
}
] }


// Document 2
{ "foo" : [
{
"shape" : "square",
"color" : "red",
"thick" : true
},
{
"shape" : "circle",
"color" : "purple",
"thick" : false
}
] }
You want to query for a purple square, and so you write the following:

db.foo.find({"foo.shape": "square", "foo.color": "purple"})
The problem with this query is that it will match the second in addition to matching the first document. In other words, the standard query syntax won't restrict itself to a single document within the foo array. As mentioned above, subobjects have to match exactly, so

db.foo.find({foo: {"shape": "square", "color": "purple"} } )
won't help either, since there's a third attribute specifying thickness.

To match an entire document within the foo array, you need to use $elemMatch. To properly query for a purple square, you'd use $elemMatch like so:

db.foo.find({foo: {"$elemMatch": {shape: "square", color: "purple"}}})
The query will return the first document, which contains the purple square you're looking for.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值