package com.leo.test;
import au.com.bytecode.opencsv.CSVReader;
import com.mongodb.*;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.types.Binary;
import org.bson.types.ObjectId;
import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author leo
* @date 2022/4/6 14:52
*/
public class LeoFileDownTest {
public static void main(String[] args) throws IOException {
ServerAddress serverAddress = new ServerAddress("127.0.0.1", 47117);
List<ServerAddress> addrs = new ArrayList<>();
addrs.add(serverAddress);
MongoCredential credential = MongoCredential.createCredential("tfsadmin", "TFS", "123456".toCharArray());
MongoClientOptions.Builder builder = MongoClientOptions.builder();
MongoClientOptions clientOptions = builder.build();
MongoClient mongoClient = new MongoClient(addrs, credential, clientOptions);
MongoDatabase tfs = mongoClient.getDatabase("TFS");
MongoCollection<Document> collection = tfs.getCollection("fs.files");
BasicDBObject query2 = new BasicDBObject();
query2.append("metadata._fileDataType_", 1);
query2.append("metadata._fileIsAnalysis_", 1);
query2.put("metadata.afterDate", new BasicDBObject("$gte", "20220328000000"));
// 查询该条件下数据的数量
long count = collection.count(query2);
System.out.println("count ---> " + count);
// 更新数据
BasicDBObject set = new BasicDBObject();
set.append("$set", new BasicDBObject("metadata._fileIsAnalysis_", 0));
collection.updateMany(query2, set);
}
}
MongoDB查询及更新数据
于 2022-04-06 14:54:05 首次发布
该博客展示了如何使用MongoDB进行数据查询和更新操作。通过连接到本地MongoDB服务器,执行查询以筛选特定条件(如文件数据类型、分析状态和时间戳)的记录,并输出查询结果数量。同时,它演示了如何批量更新这些记录的分析状态字段。
327

被折叠的 条评论
为什么被折叠?



