我正在尝试从一个集合中查询并使用 Java API 插入另一个集合,但我遇到了一个我不明白的异常:
Exception in thread "main" java.lang.IllegalArgumentException: can't save partial objects
at com.mongodb.DBCollection._checkObject(DBCollection.java:1380)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:222)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:205)
at com.mongodb.DBCollection.insert(DBCollection.java:57)
at com.mongodb.DBCollection.insert(DBCollection.java:100)
我需要在文档上调用一些“最终确定”方法吗?我的代码是这样的:
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
Mongo mongo = new Mongo("mongodb://...");
DB db = mongo.getDB("foo");
DBCollection rawCollection = db.getCollection("foo1");
DBCollection aggCollection = db.getCollection("foo2");
DateTimeZone tz = DateTimeZone.forOffsetHours(-5);
BasicDBObject toGrab = new BasicDBObject("Time1", 1).append("col2", 1).append("col3", 1);
DBCursor c = rawCollection.find(null, toGrab).limit(10);
for (DBObject doc : c) {
Date newDate = new DateTime( ((BasicBSONObject) doc).getDate("Time1") )
.withZone(tz).monthOfYear().roundCeilingCopy()
.withZone(DateTimeZone.UTC).toDate();
doc.put("Time2", newDate);
aggCollection.insert(doc);
}