$push and $set in same MongoDB update

雨燕双飞 提交于 2019-11-30 06:15:40

I don't know Java driver, but do you have to create a list there? What happens if you try this code?

BasicDBObject update = new BasicDBObject().append("$push", new BasicDBObject().append("values", dboVital));
update = update.append("$set", new BasicDBObject().append("endTime", time));

collection.update( new BasicDBObject().append("_id", pageId), update, true, false);

This should produce the equivalent of

db.collection.update({_id: pageId}, {$push: {values: dboVital}, $set: {endTime: time}});

Whereas your code produces (I suspect) this:

db.collection.update({_id: pageId}, [{$push: {values: dboVital}}, {$set: {endTime: time}}]);

My mongodb version is 3.4.20 and while using

db.collection.update({_id: pageId}, [{$push: {values: dboVital}}, {$set: {endTime: time}}]);

I received error

[thread1] Error: field names cannot start with $ [$push] :

To solve that error we can use:

db.collection.update({_id: pageId}, {$push: {values: dboVital}, $set: {endTime: time}});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!