2010-10-04 55 views
6

如何在MongoDB中搜索具有給定屬性的任何文檔?MongoDB按屬性名稱搜索任何具有該屬性的文檔

我想要做的是找到所有具有該屬性的文檔,而不管它的價值和我似乎無法做到這一點。我試過以下內容

db.collection.find({"property", null}); //Finds things that don't have that property 
db.collection.find({"proprety", {}}); //Doesn't find anything unless something has the empty object as the value 

實際上是否存在此語法或需要執行mapreduce操作?

回答

7

只是反轉的查詢和搜索文檔,其中屬性不爲null($ NE不等於)

db.collection.find({ property : { $ne : null } }); 
+3

已經搜查一些它看起來像$存在也做同樣的工作 – RobV 2010-10-04 16:39:52

+0

你是對的。 $存在真的是要走的路! – halfdan 2010-10-04 19:19:47

5

下面是使用$存在樣品答案:

db.collection.find({ property : { $exists: true } });