我正在调试涉及MongoDB和两个程序的奇怪情况。
有问题的MongoDB安装最初是空的,是一个玩具部署。
有两个程序。一个使用Morphia。另一个没有。
非Morphia使用程序将记录插入到Service
数据库中的foo-bar
集合中,如下所示:
mongo << EOF
db.getSiblingDB("foo-bar").getCollection("Service").replaceOne( {
"service_name" : "argle-bargle"
}, {
"_id" : "argle-bargle",
"className" : "com.foo.Service",
"service_name" : "argle-bargle",
}, {
"upsert" : true
} )
EOF
写入被承认:
MongoDB shell version v3.4.6
connecting to: mongodb://aura-mongodb:27017/
MongoDB server version: 3.4.6
{
"acknowledged" : true,
"matchedCount" : 0,
"modifiedCount" : 0,
"upsertedId" : "argle-bargle"
}
bye
Morphia使用程序执行此操作:
final MongoClient client = // ...
final Morphia morphia = new Morphia();
morphia.mapPackage("com.foo");
final Datastore dataStore = morphia.createDatastore(client, "foo-bar");
assert dataStore != null;
dataStore.ensureIndexes();
…其次是:
final Collection<com.foo.Service> services = // ...collection of size 2...
this.dataStore.save(services);
现在数据库中应该有三个项目。没有。
连接mongo
并在Service
数据库中获取foo-bar
集合并发布find()
显示有两个(Morphia使用程序的操作结果)。
我希望,如果我在该集合中有20,000条记录,那么save(collectionOf2Items)
就不会删除19,998条记录!
那么:当save
交给Iterable
时,save()
是破坏性的吗?关于如何调试这个令人困惑的问题的任何其他建议非常感谢。
很难说没有任何代码,但如果项目的ID匹配调用Morhpia Quick Tour将只更新现有项目。
来自qazxswpoi
更新MongoDB中的数据就像更新Java对象,然后再次使用它们调用datastore.save()一样简单。
编辑:对于调试,尝试手动将ID字段设置为您尚未使用的ID可能是值得的。