I'm trying to use MongoDB's Java driver to make two updates ($set and $push) to a record in the same operation. I'm using code similar to the following:
BasicDBObject pushUpdate = new BasicDBObject().append("$push", new BasicDBObject().append("values", dboVital));
BasicDBObject setUpdate = new BasicDBObject().append("$set", new BasicDBObject().append("endTime", time));
BasicDBList combinedUpdate = new BasicDBList();
combinedUpdate.add( pushUpdate);
combinedUpdate.add( setUpdate);
collection.update( new BasicDBObject().append("_id", pageId), combinedUpdate, true, false);
When I combine the $set and $push into the same update via a BasicDBList, I get an IllegalArgumentException: "fields stored in the db can't start with '$' (Bad Key: '$push')".
If I make two separate updates, both pushUpdate and setUpdate produce valid results.
Thanks!
I don't know Java driver, but do you have to create a list there? What happens if you try this code?
BasicDBObject update = new BasicDBObject().append("$push", new BasicDBObject().append("values", dboVital));
update = update.append("$set", new BasicDBObject().append("endTime", time));
collection.update( new BasicDBObject().append("_id", pageId), update, true, false);
This should produce the equivalent of
db.collection.update({_id: pageId}, {$push: {values: dboVital}, $set: {endTime: time}});
Whereas your code produces (I suspect) this:
db.collection.update({_id: pageId}, [{$push: {values: dboVital}}, {$set: {endTime: time}}]);
BasicDBObject update = new BasicDBObject().append("$push", new BasicDBObject().append("values", dboVital));
update = update.append("$set", new BasicDBObject().append("endTime", time));
collection.update( new BasicDBObject().append("_id", pageId), update, true, false);
My mongodb version is 3.4.20 and while using
db.collection.update({_id: pageId}, [{$push: {values: dboVital}}, {$set: {endTime: time}}]);
I received error
[thread1] Error: field names cannot start with $ [$push] :
To solve that error we can use:
db.collection.update({_id: pageId}, {$push: {values: dboVital}, $set: {endTime: time}});
Related
db.getCollection('test').update({
},
[{$set:
{
books: {$concatArrays:[
[],
[{
"status": "available",
"shelf_id": "$shelf_id",
"rack_no": "$rack_no",
"book_id": new ObjectId()
}]
]}
}
}])
I am trying to convert the above query in Java. I am getting BatchCombiner exception.
Document findDoc = new Document();
LinkedList<UpdateManyModel<Document>> docs = new LinkedList<UpdateManyModel<Document>>();
String docId = new ObjectId().toString();
Document objectIDDoc = new Document("book_id", docId).append("status", "active")
.append("rack_no", "$rack_no");
ArrayList<ArrayList<Document>> documents = new ArrayList<ArrayList<Document>>();
ArrayList<Document> instanceList = new ArrayList<Document>();
instanceList.add(objectIDDoc);
documents.add(new ArrayList<Document>());
documents.add(instanceList);
Document instancesFinalDoc = new Document("instances", new Document("$concatArrays", documents));
Document docToBeUpdated = new Document("$set", instancesFinalDoc);
I am not sure how to convert the list to Bson. Could anyone please help?
I am getting the below error:
com.mongodb.MongoBulkWriteException: Bulk write operation error on server vmwahcdb09-stg.corp.netapp.com:27016. Write errors:
[BulkWriteError{index=0, code=52, message='The dollar ($) prefixed field '$concatArrays' in 'instances.$concatArrays' is not valid for storage.', details={}}].
You are almost there.
The error is reproducible if you try to update using older driver version. You need to do as below.
Create a ClientSession
ClientSession session = mongoClient.startSession()
Then, whatever you have is perfectly correct. But You need to use updateMany latest one which supports aggregate
writeCollection.updateMany(session, findBson, updateList, updateOptions);
For your case, you need to add docToBeUpdated to a List<Document> and use as updateList.
MongoDB 2.5 driver have DBCollection.findAndModify() method for this, but MongoCollection misses this method. After some search, I found that findOneAndUpdate() now has the same role.
But this method has different signature, don't understand how to use it. Here is command I want to execute
db.COL1.findAndModify({
query: { id: 2 },
update: {
$setOnInsert: { date: new Date(), reptype: 'EOD' }
},
new: true, // return new doc if one is upserted
upsert: true // insert the document if it does not exist
})
Documentation for findOneAndUpdate method states that
Returns:
the document that was updated. Depending on the value of the returnOriginal property, this will either be the document as it was before the update or as it is after the update.
but cannot find anything about this returnOriginal property. Anyone knows how to set it correctly?
A Java equivalent of your query should go roughly like this:
Document query = new Document("id", 2);
Document setOnInsert = new Document();
setOnInsert.put("date", new Date());
setOnInsert.put("reptype", "EOD");
Document update = new Document("$setOnInsert", setOnInsert);
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions();
options.returnDocument(ReturnDocument.AFTER);
options.upsert(true);
db.getCollection("COL1").findOneAndUpdate(query, update, options);
Regarding the returnOriginal property - you're right - there is no such thing. The javadoc is irrelevant in this place. However, there is a returnDocument property in FindOneAndUpdateOptions. You can set it to ReturnDocument.AFTER or ReturnDocument.BEFORE which is equivalent to new: true/false.
I have JSON object :
{ "_id" : "1", "_class" : "com.model.Test", "projectList" : [ {
"projectID" : "Spring", "resourceIDList" : [ "Mark","David" ] },
{ "projectID" : "MongoDB", "resourceIDList" : [ "Nosa ] }
I need to be able to remove the resourceIDList for Project "Spring" and assign a new ResourceIDList.
The ResourceIDList is just List
Whenever I try to use the following,nothing is updated at DB:
Query query = new Query(where("_id").is("1").and("projectID").is("Spring"));
mongoOperations.updateMulti( query,new Update().set("ressourceIDList", populateResources()), Test.class);
Replacing the resourceIDList in the embedded document matching {"projectList.projectID":"Spring"} may be accomplished in the JavaScript shell like so:
(I like to start with the JS shell, because it is less verbose than Java and the syntax is relatively straightforward. Examples with JS can then be applied to any of the language drivers.)
> db.collection.update({_id:"1", "projectList.projectID":"Spring"}, {$set:{"projectList.$.resourceIDList":["Something", "new"]}})
The documentation on using the "$" operator to modify embedded documents may be found in the "The $ positional operator" section of the "Updating" documentation:
http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator
There is more information on embedded documents in the "Dot Notation (Reaching into Objects)" Documentation:
http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29
The above may be done in the Java Driver like so:
Mongo m = new Mongo("localhost", 27017);
DB db = m.getDB("test");
DBCollection myColl = db.getCollection("collection");
ArrayList<String> newResourceIDList = new ArrayList<String>();
newResourceIDList.add("Something");
newResourceIDList.add("new");
BasicDBObject myQuery = new BasicDBObject("_id", "1");
myQuery.put("projectList.projectID", "Spring");
BasicDBObject myUpdate = new BasicDBObject("$set", new BasicDBObject("projectList.$.resourceIDList", newResourceIDList));
myColl.update(myQuery, myUpdate);
System.out.println(myColl.findOne().toString());
If you have multiple documents that match {"projectList.projectID" : "Spring"} you can update them at once using the multi = true option. With the Java driver it would look like this:
myColl.update(myQuery, myUpdate, false, true);
In the above, "false" represents "upsert = false", and "true" represents "multi = true". This is explained in the documentation on the "Update" command:
http://www.mongodb.org/display/DOCS/Updating#Updating-update%28%29
Unfortunately, I am not familiar with the Spring framework, so I am unable to tell you how to do this with the "mongoOperations" class. Hopefully, the above will improve your understanding on how embedded documents may be updated in Mongo, and you will be able to accomplish what you need to do with Spring.
I'm using the MongoDB Java driver, and can't seem to get a query to work. I have a collection named "Questions", which has entries that look like:
{
"question" : "how are you",
"category" : "personal",
"processed" : false,
"training" : true
}
When running the Mongo command line client, the query
db.questions.find()
or
db.questions.find({"processed" : false, "training" : true})
results are returned as expected; however, my java code does the following:
DBObject queryObj = new BasicDBObject();
queryObj.put("processed", false);
queryObj.put("training", isTrain);
DBObject updateObj = new BasicDBObject();
queryObj.put("processed", true);
DBCursor cursor = mongoCollection.find(queryObj).limit(NUM_TO_LOAD);
mongoCollection.update(queryObj, updateObj);
and the cursor that is returned to me is empty/the update doesn't make any changes. If I remove the queryObj argument from the call to find, results are once again returned as expected. Am I doing something wrong here?
Thanks,
Chris Covert
looks like, you used the wrong variable on line 6. Shouldn't it be updateObj, instead of queryObj?
Hope isTrain is a boolean, and got correctly initiated with a value (true/false).
this question is very similar to another post
I basically want to use the mongodb version of the sql "like" '%m%' operator
but in my situation i'm using the java api for mongodb, while the other post is using mongodb shell
i tried what was posted in the other thread and it worked fine
db.users.find({"name": /m/})
but in java, i'm using the put method on the BasicDBObject and passing it into the find() method on a DBCollections object
BasicDBObject q = new BasicDBOBject();
q.put("name", "/"+m+"/");
dbc.find(q);
but this doesn't seem to be working.
anyone has any ideas?
You need to pass an instance of a Java RegEx (java.util.regex.Pattern):
BasicDBObject q = new BasicDBObject();
q.put("name", java.util.regex.Pattern.compile(m));
dbc.find(q);
This will be converted to a MongoDB regex when sent to the server, as well as any RegEx flags.
You must first quote your text and then use the compile to get a regex expression:
q.put("name", Pattern.compile(Pattern.quote(m)));
Without using java.util.Pattern.quote() some characters are not escaped.
e.g. using ? as the m parameter will throw an exception.
To make it case insensitive:
Document doc = new Document("name", Pattern.compile(keyword, Pattern.CASE_INSENSITIVE));
collection.find(doc);
In spring data mongodb, this can be done as:
Query query = new Query();
query.limit(10);
query.addCriteria(Criteria.where("tagName").regex(tagName));
mongoOperation.find(query, Tags.class);
Document doc = new Document("name", Pattern.compile(keyword));
collection.find(doc);
Might not be the actual answer, ( Executing the terminal query directly )
public void displayDetails() {
try {
// DB db = roleDao.returnDB();
MongoClient mongoClient = new MongoClient("localhost", 5000);
DB db = mongoClient.getDB("test");
db.eval("db.test.update({'id':{'$not':{'$in':[/su/]}}},{$set:{'test':'test3'}},true,true)", new Object[] {});
System.out.println("inserted ");
} catch (Exception e) {
System.out.println(e);
}
}
if(searchType.equals("employeeId"))
{
query.addCriteria(Criteria.where(searchType).regex(java.util.regex.Pattern.compile(searchValue)));
employees = mongoOperations.find(query, Employee.class, "OfficialInformation");
}