0

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

1 Answer 1

2

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?

Sign up to request clarification or add additional context in comments.

1 Comment

It looks like the OP just needed help creating the DBObject, but to answer your question, $cond is one of the aggregation framework expressions (like a SQL IF() function).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.