我有一个 Mongo DB COllection,如图所示
{
"name": "SAM",
"collection": [
{
"date": "2013-03-16",
"values": [
{
"price": "24.0"
}
]
},
{
"date": "2013-04-20",
"values": [
{
"price": "10.0"
}
]
},
{
"date": "2013-05-18",
"values": [
{
"price": "12.0"
}
]
},
{
"date": "2013-06-22",
"values": [
{
"price": "10.0"
}
]
},
{
"date": "2013-09-21",
"values": [
{
"price": "38.0"
}
]
}
]
}
我试图获取与特定日期(2013-03-16)相关的数据,如图所示,但我正在获取所有日期的数据。
这就是我尝试过的。
请告诉我我在哪里做错了。
package com;
import java.net.UnknownHostException;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
public class Test {
public static void main(String args[]) throws UnknownHostException {
Mongo mongo = new Mongo();
DB db = mongo.getDB("test");
DBCollection mycollection = db.getCollection("mycollection");
BasicDBObject query = new BasicDBObject();
query.put("name", "SAM");
query.put("collection.date", "2013-03-16");
DBCursor cursor = mycollection.find(query);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
}
}