简体   繁体   中英

MongoDB aggregation condition translated to JAVA driver

I'm relatively new to MongoDB and I'm currently working with java towards a "find by most tags matching" solution to information within a collection.

I'm stuck now trying to translate a MongoDB shell operation to the JAVA driver version (this sintaxis is part of the definitions needed )

$cond:[{$eq: ["$tags", 200]}, 1, 0]

What would be a correct JAVA implementation for the sentence above?

Thank you in advance

Whatever the $cond object where in your aggregation operation, to build it to should do something like this:

BasicDBList eqList = new BasicDBList();
eqList.add("$tags");
eqList.add(200);

DBObject eqObject = BasicDBObjectBuilder.start()
    .add("$eq", eqList)
    .get();

BasicDBList condList = new BasicDBList();
condList.add(eqObject);
condList.add(1);
condList.add(0);

DBObject condObject = BasicDBObjectBuilder.start()
    .add("$cond", condList)
    .get();

I'm confusing about your aggregation operation, could you give more details?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM