搜索包含一個元素的數組,你可以檢查對數組元素。
所以,只要使用: -
db.collection.find({transport:"plane"})
會給你想要的東西。
以下是一個Java的實現: -
BasicDBObject doc = new BasicDBObject("document_id", 35)
.append("transport", new String[] {"car", "plane"});
BasicDBObject doc2 = new BasicDBObject("document_id", 36)
.append("transport", new String[] {"car"});
coll.insert(doc);
coll.insert(doc2);
DBObject query = new BasicDBObject("transport", "plane");
DBCursor cursor = coll.find(query, new BasicDBObject("_id", 0));
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
輸出: -
{ "document_id" : 35 , "transport" : [ "car" , "plane"]}