Nima
Nima

Reputation: 113

How to delete document in mongodb using two conditions in java

I would like to retrieve the following information:

delete from mytable where name = 'aaa' and title = 'mongodb';

but for MongoDB in Java. Essentially, it should delete the specific document that contain the word aaa and title mongodb in them. I know that there is the $in operator in MongoDB, but how do I do the same in Java, using the Java driver? I've been trying to look for it everywhere but am getting nothing.

This is my required JSONObject to delete:

{
    "@collection": "mytable",
    "name": "aaa",
    "title": "mongodb",
    "likes": "100" 
}

I've tried:

query = new BasicDBObject("name", new BasicDBObject("$in", "aaa"), new BasicDBObject("title", "mongodb"));

Upvotes: 1

Views: 551

Answers (1)

JVXR
JVXR

Reputation: 1312

If you want and exact match, you can use the query

DBObject query = new BasicDbObject();
query.put("name","aaa");
query.put("title","mongodb");

If you want a partial match use the query

DBObject query = new BasicDbObject();
query.put("name", java.util.regex.Pattern.compile("/.*aaa.*/"));
query.put("title","mongodb");

Upvotes: 1

Related Questions