Use MongoDB db.collection.remove(query) in java - java

Is there a way in the MongoDB Java driver to call the db.collection.remove(query) method that I see in the MongoDB shell documentation?
That is, I know the exact criteria that I need to find all the documents I want to delete from MongoDB, but I can't find a way to make one call to remove those records in one trip. All that I can figure out is to find the documents and then delete them one by one.
I see this
http://docs.mongodb.org/manual/reference/method/db.collection.remove/
which implies there should be a way to do it, but I can't figure out the Java calls to get me that to that call.
Thank you for your help

To remove documents with an age property of 25.
MongoClient mongo = new MongoClient(new ServerAddress("localhost", 27017));
DB db = mongo.getDB("thedb");
DBCollection collection = db.getCollection("test");
BasicDBObject query = new BasicDBObject();
query.append("age", 25);
collection.remove(query);
DBCollection and BasicDBObject are two of the most important classes in the Java API.

Also to remove specific values from you document you can use following code with Mongo Java 3.2
Document docToDelete = new Document("Designation", "SE-1");
objDbCollection.findOneAndUpdate(new Document("Company", "StackOverflow"), new Document("$unset", docToDelete));
Above code will first find document having company = StackOverflow and then unset (remove) Designation = SE-1 key/value from that document.

Add and Update Mongo
public class App {
public static void main(String[] args) {
try {
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("yourdb");
// get a single collection
DBCollection collection = db.getCollection("dummyColl");
//insert number 1 to 10 for testing
for (int i=1; i <= 10; i++) {
collection.insert(new BasicDBObject().append("number", i));
}
//remove number = 1
DBObject doc = collection.findOne(); //get first document
collection.remove(doc);
//remove number = 2
BasicDBObject document = new BasicDBObject();
document.put("number", 2);
collection.remove(document);
//remove number = 3
collection.remove(new BasicDBObject().append("number", 3));
//remove number > 9 , means delete number = 10
BasicDBObject query = new BasicDBObject();
query.put("number", new BasicDBObject("$gt", 9));
collection.remove(query);
//remove number = 4 and 5
BasicDBObject query2 = new BasicDBObject();
List<Integer> list = new ArrayList<Integer>();
list.add(4);
list.add(5);
query2.put("number", new BasicDBObject("$in", list));
collection.remove(query2);
//print out the document
DBCursor cursor = collection.find();
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
collection.drop();
System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}

Related

How to combine $in and max in MongoDb?

I want to get max timestamp of a set of tags from MongoDb history database. Say the tag ids are 1,2,3,4,5 I want to check all records for these tags and get the timestamp of latest. My collection looks like this along with data:
My code is as follows:
protected Timestamp getMaxRealTimeHistoryTimestamp(List<Integer> tagIds)
{
try
{
MongoClient mongo = new MongoClient(m_connectionInfo.getHost(), m_connectionInfo.getPort());
//Connecting to the database
MongoDatabase database = mongo.getDatabase(m_connectionInfo.getDatabaseName());
BasicDBObject andQuery = new BasicDBObject();
List<BasicDBObject> obj = new ArrayList<>();
obj.add(new BasicDBObject("TAG_ID", new BasicDBObject("$in", tagIds)));
MongoCollection<Document> collection = database.getCollection("EM_HISTORY");
Document doc = collection.find(andQuery).sort(new Document("TIME_STAMP", -1)).first();
if(doc != null)
{
return new Timestamp(((Date) doc.get("TIME_STAMP")).getTime());
}
}
catch (Exception e)
{
if (Logger.isErrorEnabled())
Logger.error(e);
}
return null;
}
the doc variable has some strange row that is not even in the collection
What am I doing wrong here?
BasicDBObject andQuery = new BasicDBObject();
List<BasicDBObject> obj = new ArrayList<>();
obj.add(new BasicDBObject("TAG_ID", new BasicDBObject("$in", tagIds)));
You are never adding the query filters from obj back into your andQuery, so the code ends up querying the collection without any filter.

Increment value in a subdocument in MongoDB [duplicate]

This question already has answers here:
How to update value of specific embedded document, inside an array, of a specific document in MongoDB?
(4 answers)
Closed 5 years ago.
I have a document in my collection
{
"_id" : ObjectId("59bd65b281a24497a0b3b401"),
"user_id" : "1",
"is_placed" : false,
"items" : [
{
"product_id" : 1,
"units" : 0
},
{
"product_id" : 2,
"units" : 0
}
]
}
I want to increment value of 'units' for product_id = 1. Here is what my code looks like:
MongoClient mongoConnection = new MongoClient("localhost", 27017);
try {
MongoDatabase db = mongoConnection.getDatabase("mydb");
MongoCollection<Document> collection = db.getCollection("orders");
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("user_id", "1");
whereQuery.put("is_placed", false);
whereQuery.put("product_id", 1);
BasicDBObject incValue = new BasicDBObject("items.units", 1);
BasicDBObject intModifier = new BasicDBObject("$inc", incValue);
collection.updateOne(whereQuery, intModifier);
} catch (Exception e) {
e.printStackTrace();
}
It does not throw any error. But neither does it increment the value. How do I achieve it? Also, is it possible to increment it only if product_id = 1 exists else put product_id = 1?
For updating an embedded document in an array, you should use $ operator. I cannot see you use it anywhere in your code. Your code may be, should be like this:
MongoClient mongoConnection = new MongoClient("localhost", 27017);
try {
MongoDatabase db = mongoConnection.getDatabase("db");
MongoCollection<Document> collection = db.getCollection("orders");
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("user_id", "1");
whereQuery.put("is_placed", false);
whereQuery.put("items.product_id", 1);
BasicDBObject incValue = new BasicDBObject("items.$.units", 1);
BasicDBObject intModifier = new BasicDBObject("$inc", incValue);
collection.updateOne(whereQuery, intModifier);
} catch (Exception e) {
e.printStackTrace();
}

Mongodb Java - How to return all fields of a collection

With the driver Java Mongodb, I am looking for a way to return all fields of one collection.For example, I have a collection "people", how can I get all fields, I want output: 'id','name','city'...
Thanks a lot, I have finally got the answer.
DBCursor cur = db.getCollection("people").find();
DBObject dbo = cur.next();
Set<String> s = dbo.keySet();
From manual:
To return all documents in a collection, call the find method without a criteria document. For example, the following operation queries for all documents in the restaurants collection.
FindIterable<Document> iterable = db.getCollection("restaurants").find();
Iterate the results and apply a block to each resulting document.
iterable.forEach(new Block<Document>() {
#Override
public void apply(final Document document) {
System.out.println(document);
}
});
You'd need to decide on the number of samples that would make sense to you but this would pull the last 10 submissions.
Document nat = new Document().append("$natural",-1);
FindIterable<Document> theLastDocumentSubmitted = collection.find().limit(10).sort(nat);
ArrayList<String>fieldkeys = new ArrayList<String>();
for (Document doc : theLastDocumentSubmitted) {
Set<String> keys = doc.keySet();
Iterator iterator = keys.iterator();
while(iterator.hasNext()){
String key = (String) iterator.next();
String value = (String) doc.get(key).toString();
if(!fieldkeys.contains(key)) {
fieldkeys.add(key);
}
}
}
System.out.println("fieldkeys" + fieldkeys.toString());
Below code will return all fields from given collection.
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("collection_name");
AggregateIterable<Document> output = mongoCollection.aggregate(Arrays.asList(
new Document("$project", Document("arrayofkeyvalue", new Document("$objectToArray", "$$ROOT"))),
new Document("$unwind", "$arrayofkeyvalue"),
new Document("$group", new Document("_id", null).append("allkeys", new Document("$addToSet", "$arrayofkeyvalue.k")))
));
Below code will return all fields of people collection :
db.people.find()

How to find documents matching multiple criteria

I'm trying to query a collection using operands AND'd together. I've got the shell version working:
db.widgets.find({color: 'black, shape: 'round', weight: 100})
I'm unable to find the Java equivalent (using the native driver). I've tried various things, but here is my latest attempt:
// Find all black, round widgets with weight 100
List<BasicDBObject> criteria = new ArrayList<BasicDBObject>();
criteria.add(new BasicDBObject("color", "black"));
criteria.add(new BasicDBObject("shape", "round"));
criteria.add(new BasicDBObject("weight", 100));
DBCursor cur = widgets.find(new BasicDBObject("$and", criteria));
// Get all matching widgets and put them into a list
List<Widget> widgetList = new ArrayList<Widget>();
DBCursor cur = widgets.find(andQuery);
while (cur.hasNext()) {
widgetList.add(new Widget(cur.next()));
}
if (widgetList.isEmpty())
System.out.println("No results found");
Any ideas what is wrong?
BasicDBObject criteria = new BasicDBObject();
criteria.append("color", "black");
criteria.append("shape", "round");
criteria.append("weight", 100);
DBCursor cur = widgets.find(criteria);
Another way to solve same problem is using aggregation:
// To print results
Block<Document> printBlock = new Block<Document>() {
#Override
public void apply(final Document document) {
System.out.println(document.toJson());
}
};
// get db connection and collection
MongoDatabase db= mongoClient.getDatabase("dbname");
MongoCollection<Document> collection= database.getCollection("collectionname");
collection.aggregate(Arrays.asList(Aggregates.match(Filters.eq("key1", "value1")),
Aggregates.match(Filters.eq("key2", "value2")),
Aggregates.match(Filters.eq("key3", "value3")))).forEach(printBlock);
For more details please refer the v 3.4 mongo Aggregation documentation.
http://mongodb.github.io/mongo-java-driver/3.4/driver/tutorials/aggregation/
I think the solution above should also work just fine, given the situation I'd suggest the following as an alternative. This is using Criteria
Query query = new Query();
List<Criteria> criteriaList = new ArrayList<>();
Criteria criteriaCreatedBy1 = new Criteria().where("title").is("input");
criteriaList.add(criteriaCreatedBy1);
Criteria criteriaCreatedBy2 = new Criteria().where("name").is("name input");
criteriaList.add(criteriaCreatedBy2);
query.addCriteria(new Criteria().andOperator(criteriaList.toArray(new Criteria[criteriaList.size()])));
List<Screen> getSearchScreens = mongoTemplate.find(query,Screen.class,"collectionName");

Upgrading mongo search from runCommand to find using Java driver

I am using a Java driver to run some mongo text searches.
An example of my previous code is (where values is a String passed in):
DBCollection coll = db.getCollection("testCollection");
//create the basic object
DBObject searchCmd = new BasicDBObject();
//create the search cmd
searchCmd.put("text", "testCollection"); // the name of the collection (string)
// define the search word
searchCmd.put("search", value); // the term to search for (string)
// define the return values
searchCmd.put("project", new BasicDBObject("score", 1).append("name", 1).append("path", 0).append("_id", 0));
// get the results
BasicDBObject commandResult = db.command(searchCmd);
// Just out the results key
BasicDBList results = (BasicDBList) commandResult.get("results");
then I loop over the "results" and I get for each it score by
// Get the number ii
BasicDBObject element = (BasicDBObject) results.get(ii);
// Now get the score
double score = (double) element.get("score");
I want to upgrade to use find since that seems the way 2.6 and later prefers it. So far I have:
DBCollection coll = db.getCollection("testCollection");
BasicDBObject query = new BasicDBObject();
query.append("$text", new BasicDBObject("$search", value));
DBCursor cursor = coll.find(query);
However, I am not sure how to get the score.
I tried doing something like:
query.append("score", new BasicDBObject("$meta", "textScore"));
But this does not work. I would like to be able to get the name and the score so that I can then insert them into a new collection that will also hold the score.
I can get the name easily by:
while (cursor.hasNext())
{
DBObject next = cursor.next();
String name = next.get("name").toString();
}
But how do I get the score?
I found this interesting page: http://api.mongodb.org/java/current/
it appears that find can take a second DBObject which has the fields.
I created a new object:
BasicDBObject fields = new BasicDBObject();
fields.append("score", new BasicDBObject("$meta", "textScore"));
and I am calling find using:
DBCursor cursor = coll.find(query, fields);
and now I can get the score the same way I can get the name.

Categories

Resources