我想通过Java代码实现以下更新功能
db.getCollection("test_rohit").update(
{},
{
$push: {
x_axis: {
$each: [ ],
$slice: -1
}
}
},
{ multi: true }
)
-
我写了下面的代码来解决它
DBObject eachObject =new BasicDBObject();
eachObject.put("$each", new String[0]);
eachObject.put("$slice", -1);
BasicDBObject pushDbObject =new BasicDBObject();
pushDbObject.append("x_axis", eachObject);
Bson modifiedObject =set("$push", pushDbObject);
Bson filter=new BasicDBObject("", "");
long result = collection.updateMany(filter, modifiedObject ).getModifiedCount();
但是它不起作用,因为我猜想过滤器对象与{}不匹配(这通常意味着对所有文档而言),所以我如何为{}创建过滤器对象。
示例文档
{
"_id" : ObjectId("5c8718b9b5fe262104408374"),
"axis" : "x",
"message_time" : ISODate("2019-03-11T08:04:41.000Z"),
"x_axis" : [
0.9766,
1.9531,
2.9297,
3.9063,
4.8828,
5.8594,
6.8359,
7.8125,
8.7891,
],
"etl_date_time" : ISODate("2019-03-12T02:26:01.510Z")
}