2014-09-03 57 views
0

这里收藏的一个例子,我在MongoDB中如何检索mongodb中所有的字段值?

{ 
    "_id":ObjectId("54076c79f764e4ea431bf7f6"), 
    "trends":[ 
    { 
    "query":"%23Ces aroni", 
    "name":"#Cesaroni", 
    "url":"http://twitter.com/search?q=%23Cesaroni", 
    "promoted_content":null 
    }, 
    { 
    "query":"%23MelihG%C3%B6kcekKKy%C4%B1De%C5%9Fi freEdiyor", 
    "name":"#MelihGökcekKKyiDesifreEdiyor", 
    "url":"http://twitter.co m/search?q=%23MelihG%C3%B6kcekKKy%C4%B1De%C5%9FifreEdiyor", 
    "promoted_content":null 
    }, 
    { 
    "query":"%23EngvNor", 
    "name":"#EngvNor", 
    "url":"http://twitter. com/search?q=%23EngvNor", 
    "promoted_content":null 
    }, 
    "locations":[ 
    { 
    "woeid":1, 
    "name":"Worldwide" 
    } 
    ], 
    "created_at":"2014-09-03T19:23:55Z", 
    "as_of":"2014-09-03T19:31:05Z" 
} 

收集转储到MongoDB的集合,因为它是从Twitter获取。现在,我想为其他位置添加更多数据。由于这是我第一次使用mongodb,我无法弄清楚我如何才能检索位置字段。

+0

你尝试过什么?你看到了什么结果?检查[db.collection.find](http://docs.mongodb.org/manual/reference/method/db.collection.find/) – dc5 2014-09-03 22:53:32

+0

@ dc5,我尝试了db.collection.find(),它返回所有文档。我也尝试了db.collection.find({locations:“name”})和其他一些不返回任何东西或者产生错误的东西。 – user1828605 2014-09-03 23:05:06

+0

您是否阅读了关于该主题的[文档](http://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/)? – JohnnyHK 2014-09-03 23:11:26

回答

2

使用投影与空或过滤查询:

db.collectionName.find({},{locations:1}) 

将检索是这样的:

{ 
"_id" : ObjectId("5407a0a9b7a33063850f5b09"), 
"locations" : [ 
    { 
     "woeid" : 1, 
     "name" : "Worldwide" 
    } 
] 
} 
+1

很酷。这就是我想要的。非常感谢你。 – user1828605 2014-09-03 23:19:19

+0

不客气! – CesarTrigo 2014-11-15 16:38:24

相关问题