1

Is there any easy way of firing Mongo query in Java??

db.Test.aggregate(
    [
    {
        '$match':
        {
        'o': { '$gt': [] }
        }
    },
    {
        '$project': {
        'uid': 1,
        'o': 1
        }
    },
    {
        '$project': {
        '_id': 0,
        'uid': 1,
        o: {
            $filter: {
            input: "$o",
            as: "item",
            cond: {
                $and: [
                {
                    $lt: [ "$$item.ad", 0 ]
                },
                {
                    $lt: [ "$$item.at", 0 ]
                }
                ]
            }
            }
        }
        }
    },
    {
        '$match': {
        'o': { '$gt': []}
        }
    },
    {
        $project: {
        uid: 1,
        "mids": "$o.mid"
        }
    },
    {
        $unwind: "$mids"
    },
    {
        $group: {
        _id: {
            uid: "$uid",
            mid: "$mids"
        },
        count: { $sum: 1 }
        }
    },
    {
        $project: {
        _id: 0,
        uid: "$_id.uid",
        mid: "$_id.mid",
        count: 1
        }
    }
    ]
);

Is http://jongo.org serve the purpose for complex queries?

1
  • I did not know jongo until now but it seems to make the querying in java a lot more palatable by using the JSON style query language. Commented Nov 27, 2017 at 20:45

1 Answer 1

1

As an alternative, you can use the Java driver's Document.parse() method. You can supply a JSON string to the method (following MongoDB's extended JSON formatting), and it will return a parsed BSON document for you.

Please see http://mongodb.github.io/mongo-java-driver/3.5/javadoc/org/bson/Document.html#parse-java.lang.String- for the method's documentation.

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

Comments

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.