使用 insertOne 和 findOneAndUpdate 时设置最后修改时间戳 [英] Setting last modification timestamp when using insertOne and findOneAndUpdate

查看:34
本文介绍了使用 insertOne 和 findOneAndUpdate 时设置最后修改时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我在 MongoDb 中插入/更新的所有文档都有一个自动更新的 当前日期

I need all my inserted/updated documents in MongoDb to have an auto-updated currentDate

所以让我们假设我有以下 Json shipment 对象(我从 3rd 方 restful API 获得):

So let's assume I have the following Json shipment object (which I'm getting from a 3rd party restful API) :

String jsonString = {"tracking_number": "123", "deliveryAddress": { "street_line_1": "12 8th St.", "city": "NY", "state": "NY" }, "cutomers": [ { "firstName": "John", "email": "john@gmail.com" }, { "firstName": "Alex", "email": "alex@gmail.com" } ] }

问题 #1,我需要将对象插入数据库并设置currentDate",但 insertOne 对我不起作用:

Problem #1, I need to insert the object into the DB and set "currentDate", but insertOne does not work for me:

    MongoClient mongo = new MongoClient(mongodb_host, mongodb_port);
    MongoDatabase db = mongo.getDatabase("Test");
    MongoCollection<Document> collection = db.getCollection("InsertOneExample");
    Document doc = Document.parse(jsonString);
    doc.append("lastModifiedTs", new BSONTimestamp());
    collection.insertOne(doc);
    System.out.println(doc);

正如您在下面看到的那样,这个不会填充lastModifiedTs"

This one does not populate "lastModifiedTs" as you can see below

Document{{tracking_number=123, deliveryAddress=Document{{street_line_1=12 8th St., city=NY, state=NY}}, cutomers=[Document{{firstName=John, email=john@gmail.com}}, Document{{firstName=Alex, email=alex@gmail.com}}], lastModifiedTs=TS time:null inc:0, _id=5a6b88a66cafd010f1f2cffd}}

问题 #2

如果我收到货件的更新,跟踪编号是相同的,但所有其他字段可能会更改.

If I'm getting an update on my shipment, the tracking number is the same, but all the other fields may change.

以下代码崩溃:

    FindOneAndUpdateOptions options = new FindOneAndUpdateOptions();
    options.returnDocument(ReturnDocument.AFTER);
    options.upsert(true);
    Bson update = Updates.combine(Document.parse(jsonString), Updates.currentTimestamp("lastModifiedTs"));
    Document query = new Document("tracking_number", "123");
    Document result = collection.findOneAndUpdate(query, update, options);

例外:无效的 BSON 字段名称 equipmentShipmentAddress"

With the exception: "Invalid BSON field name equipmentShipmentAddress"

  • 所以看起来我不能将整个更新的文档放入更新"
  • 如果我只将 update 设置为 Updates.currentTimestamp("lastModifiedTs"),代码将只更新字段lastModifiedTs",但我需要它来修改所有字段.
  • 如果我将查询设置为新对象,那么由于我的upsert"设置,它会添加新文档而不替换旧文档.

注意:不用说,我可以执行几种操作:(1)插入对象,获取_id"字段,(2)更新lastModifiedTs"字段(3)通过_id"读取对象并获取更新后的lastModifiedTs"值,但这是三个操作,我希望能够通过单个操作实现所有功能

Notes: needless to say, I can perform several operations: (1) insert the object, get the "_id" field, (2) update the "lastModifiedTs" field (3) read the object by "_id" and get the updated "lastModifiedTs" value, but it's three operation where I expect to be able to achieve everything with a single operation

我怎样才能优雅地实现我的目标?

How can I achieve my goal elegantly?

谢谢

推荐答案

问题 #1 的解决方案 - 插入 new Date() 以提供新的日期时间.

Solution to Problem #1 - Insert new Date() to provide new datetime.

Document doc = Document.parse(jsonString);
doc.append("lastModifiedTs", new Date());
collection.insertOne(doc);

问题 #2 的解决方案 - 使用 findOneAndReplace

Solution to Problem #2 - Use findOneAndReplace

FindOneAndReplaceOptions options = new FindOneAndReplaceOptions();
options.returnDocument(ReturnDocument.AFTER);
Document replace = Document.parse(jsonString);
replace.append("lastModifiedTs", new Date());
Document query = new Document("tracking_number", "123");
Document result = collection.findOneAndReplace(query, replace, options);

这篇关于使用 insertOne 和 findOneAndUpdate 时设置最后修改时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆