如何使用 java 代码转换 mongodb 查询
How to Transform mongodb query using java code
我是 MongoDB 的新手。我想将此 mongo 数据库查询转换为 java 代码。我已经尝试了一点。但我有一个问题。请,如果有人帮助意味着这对我来说将是一个很大的帮助。提前致谢。
db.getCollection('test').aggregate(
[{
$match: {
"ids": 9999999,
$or : [{"tags.name":"roja"},{"tags.location":"US"},{"tags.year":2019}]
}
},
{
'$facet': {
metadata: [{ $count: "total" }],
data: [{
$addFields: {
"weight": {
$map: {
input: "$tags",
as: "tagsEl",
in: {
"$add":
[
{$cond: [ { $eq: [ '$$tagsEl.roja', 'roja' ] }, 15, 1 ]} ,
{$cond: [ { $eq: [ '$$tagsEl.location', 'US' ] }, 10, 1 ]},
{$cond: [ { $eq: [ '$$tagsEl.year', 2019 ] }, 5, 1 ]}
]
}
}
}
}
}, { $skip: 0 }, { $limit: 10 }, { '$sort': { 'weight' : -1 } }]
}
}
]
)
您可以使用 Studio3T 将您的查询直接转换为 java 代码,除非您对语法不满意。
//Get database
MongoDatabase db = mongoClient.getDatabase("testdb");
//get collection
MongoCollection<Document> testCol = db.getCollection("test");
//prepare a list of aggregation pipeline stages.
List<Bson> aggs = new ArrayList<>();
//each pipeline stage you wrote is in js. the equivalent java syntax is...
//for {key1:value1,key2:value2}
new Document().append("key1","value1").append("key2","value2")
//for array such as ["abc",1,{key3,value3}]
Arrays.asList("abc",1,new Document("key3","value3"))
//for example , part of your script can be implemented in java as below.
aggs.add(
new Document("$match",//match aggregation pipeline stage
new Document("ids",9999999)
.append("$or",Arrays.asList(
new Document("tags.name","roja"),
new Document("tags.location","US"),
new Document("tags.year",2019)
))
)
);
//you can add as many stages as you need to aggs list.
//execute
MongoCursor<Document> cursor = testCol.aggregate(aggs).iterator();
while(cursor.hasNext()){
Document d = cursor.next();
//do your operation.
}
我是 MongoDB 的新手。我想将此 mongo 数据库查询转换为 java 代码。我已经尝试了一点。但我有一个问题。请,如果有人帮助意味着这对我来说将是一个很大的帮助。提前致谢。
db.getCollection('test').aggregate(
[{
$match: {
"ids": 9999999,
$or : [{"tags.name":"roja"},{"tags.location":"US"},{"tags.year":2019}]
}
},
{
'$facet': {
metadata: [{ $count: "total" }],
data: [{
$addFields: {
"weight": {
$map: {
input: "$tags",
as: "tagsEl",
in: {
"$add":
[
{$cond: [ { $eq: [ '$$tagsEl.roja', 'roja' ] }, 15, 1 ]} ,
{$cond: [ { $eq: [ '$$tagsEl.location', 'US' ] }, 10, 1 ]},
{$cond: [ { $eq: [ '$$tagsEl.year', 2019 ] }, 5, 1 ]}
]
}
}
}
}
}, { $skip: 0 }, { $limit: 10 }, { '$sort': { 'weight' : -1 } }]
}
}
]
)
您可以使用 Studio3T 将您的查询直接转换为 java 代码,除非您对语法不满意。
//Get database
MongoDatabase db = mongoClient.getDatabase("testdb");
//get collection
MongoCollection<Document> testCol = db.getCollection("test");
//prepare a list of aggregation pipeline stages.
List<Bson> aggs = new ArrayList<>();
//each pipeline stage you wrote is in js. the equivalent java syntax is...
//for {key1:value1,key2:value2}
new Document().append("key1","value1").append("key2","value2")
//for array such as ["abc",1,{key3,value3}]
Arrays.asList("abc",1,new Document("key3","value3"))
//for example , part of your script can be implemented in java as below.
aggs.add(
new Document("$match",//match aggregation pipeline stage
new Document("ids",9999999)
.append("$or",Arrays.asList(
new Document("tags.name","roja"),
new Document("tags.location","US"),
new Document("tags.year",2019)
))
)
);
//you can add as many stages as you need to aggs list.
//execute
MongoCursor<Document> cursor = testCol.aggregate(aggs).iterator();
while(cursor.hasNext()){
Document d = cursor.next();
//do your operation.
}