st0627 发表于 2015-7-10 10:40:35

[Database] MongoDB (4) Dot Notation 内嵌对象查询

  Dot Notation (Reaching into Objects)

  
  查看数据





In : list(t.find())
Out:
[{u'_id': ObjectId('4c6bf0fb421aa90705000000'),
u'address': {u'city': u'shandong', u'postcode': 350, u'state': u'china'},
u'age': 28,
u'email': u'abeen_8298@msn.com',
u'name': u'abeen'},
{u'_id': ObjectId('4c6bf125421aa90705000001'),
u'address': {u'city': u'beijing', u'postcode': 64, u'state': u'china'},
u'age': 22,
u'email': u'tangtang_0902@gmail.com',
u'name': u'shanshan'}]
  
查找"address的city"为"shandong"




In : list(t.find({"address.city":"shandong"}))
Out:
[{u'_id': ObjectId('4c6bf0fb421aa90705000000'),
u'address': {u'city': u'shandong', u'postcode': 350, u'state': u'china'},
u'age': 28,
u'email': u'abeen_8298@msn.com',
u'name': u'abeen'}]
  
利用嵌入对象信息条件查找(嵌入对象的key和value必须全使用)




In : list(t.find({"address":{"city": "shandong", "postcode": 350, "state": "china"}}))
Out:
[{u'_id': ObjectId('4c6bf0fb421aa90705000000'),
u'address': {u'city': u'shandong', u'postcode': 350, u'state': u'china'},
u'age': 28,
u'email': u'abeen_8298@msn.com',
u'name': u'abeen'}]
  

  Array Element by Position
数组元素按位置查找




In : list(t.find({"name":"meinv"}))
Out:
[{u'_id': ObjectId('4c6bf67c421aa9075c000000'),
u'age': 46,
u'likes': ,
u'name': u'meinv'},
{u'_id': ObjectId('4c6bf7a4421aa9075c000001'),
u'age': 46,
u'comments': [{u'by': u'abeen', u'title': u'this is the title'},
                {u'by': u'shanshan', u'title': u'shuo wo ni shi shi'}],
u'name': u'meinv'}]
  
查找爱好的第一项是"grape"的




In : list(t.find({"likes.1": "grape"}))
Out:
[{u'_id': ObjectId('4c6bf67c421aa9075c000000'),
u'age': 46,
u'likes': ,
u'name': u'meinv'}]
  
查找第一条评论是"abeen"发的信息




In : list(t.find({"comments.0.by": "abeen"}))
Out:
[{u'_id': ObjectId('4c6bf7a4421aa9075c000001'),
u'age': 46,
u'comments': [{u'by': u'abeen', u'title': u'this is the title'},
                {u'by': u'shanshan', u'title': u'shuo wo ni shi shi'}],
u'name': u'meinv'}]
  
  

Matching with $elemMatch
未完
页: [1]
查看完整版本: [Database] MongoDB (4) Dot Notation 内嵌对象查询