mongodb - 在 upsert 期间使用 $set 和 $setOnInsert

标签 mongodb mongodb-query mongodb-java

我正在尝试更新 mongodb 中的集合,如果字段存在,将插入它。我不太确定如何使用 upsert 选项做到这一点。

MongoCollection<Document> docs = mongoDb.getCollection("users");
Bson filterQuery = new Document("userId", userId).append("fileType", fileType);
Bson updateQuery = new Document("$set", new Document("fileType", fileType).append("fileName", fileName).append("fileSize", fileSize).append("userId", userId).append("lastModifiedTimestamp", new Timestamp(System.currentTimeMillis())));
updateQuery.append("$setOnInsert", new Document("creationTimestamp", new Timestamp(System.currentTimeMillis())));
docs.updateOne(filterQuery, updateQuery, (new UpdateOptions()).upsert(true));

这是行不通的。我没有为 $set 和 $setOnInsert 更新相同的字段,但不确定为什么这不起作用。有什么想法吗?

最佳答案

尝试使用以下配置,我在下面修复了您的代码中的一些编译错误。

public class StackOverflowApp {

   public static void main (String[] args){

    MongoClientOptions options = MongoClientOptions.builder().connectionsPerHost(100).build();
    MongoClient client = new MongoClient(new ServerAddress(), options);
    MongoDatabase db = client.getDatabase("test").withReadPreference(ReadPreference.secondary());

    String userId = "myUser2";
    String fileType = "fileType2";
    String fileName = "fileName";
    String fileSize = "fileSize";
    MongoCollection<Document> docs = db.getCollection("users");
    Bson filterQuery = new Document("userId", userId).append("fileType", fileType);
    Bson updateQuery = new Document("$set", new Document("fileType", fileType).append("fileName", fileName).append("fileSize", fileSize).append("userId", userId).append("lastModifiedTimestamp", new Date(System.currentTimeMillis())));
    ((Document)updateQuery).append("$setOnInsert", new Document("creationTimestamp", new Date(System.currentTimeMillis())));
    docs.updateOne(filterQuery, updateQuery, (new UpdateOptions()).upsert(true));
    }
}

如果我第一次运行,我会插入这个。

"_id" : ObjectId("570c6f2105c7937ac1c794cb"),
"fileType" : "fileType2",
"userId" : "myUser2",
"fileName" : "fileName",
"fileSize" : "fileSize",
"lastModifiedTimestamp" : ISODate("2016-04-12T03:44:33.454Z"),
"creationTimestamp" : ISODate("2016-04-12T03:44:33.454Z")

仅第二次更新 lastModifiedTimestamp

"_id" : ObjectId("570c6f2105c7937ac1c794cb"),
"fileType" : "fileType2",
"userId" : "myUser2",
"fileName" : "fileName",
"fileSize" : "fileSize",
"lastModifiedTimestamp" : ISODate("2016-04-12T03:44:59.947Z"),
"creationTimestamp" : ISODate("2016-04-12T03:44:33.454Z")

我认为这就是您试图实现的目标。

关于mongodb - 在 upsert 期间使用 $set 和 $setOnInsert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36563119/

相关文章:

mongodb - Spring-Data mongodb查询存储在同一个集合中的多个类

mongodb - spring-data-mongodb 给出 mongodb 身份验证异常

java - MongoDB 在 Java 中映射引用关系

mongodb - Golang MGO - 插入或更新未按预期工作

mongodb - 查找具有最接近整数值的文档

node.js - MongoClient.connect - 错误: Could not locate any valid servers in initial seed list

node.js - 如何正确使用mongoDB中的查找和更新?

javascript - 在 mongodb 中搜索嵌入式评论

java - 在 Spring Data Mongo 中设置自定义转换器

javascript - 使用 npm install --save mongodb 安装 mongo db