I have a class that contains a List as one of the field. How can I update this field.
Found an example for updating a field
BasicDBObject newDocument3 = new BasicDBObject().append("$set", new
BasicDBObject().append("type", "dedicated server"));
collection.update(new BasicDBObject().append("hosting", "hostA"), newDocument3);
From link -> http://www.mkyong.com/mongodb/java-mongodb-update-document/
So this is what I have tried
BasicDBObject objectUpdateCommand = new BasicDBObject("$push", new
BasicDBObject("someList", stringValue));
collection.update(new BasicDBObject().append("id", user.getId()).append("email",
user.getEmail()), objectUpdateCommand);
Result: No change.
What am I missing?
Tried on shell and it worked [I know its not matching all the ids but it works for test purpose]
db.user.update( { Id: 'yourid'}, {$push: { someList: 'appendNewValue'} } )
I have inserted the following rows into the collection.
{ "_id" : ObjectId("50bc89ef88555f5ad35da8ba"), "id" : 1, "email" : "test1#test.com", "list" : [ "list1", "list2" ] }
{ "_id" : ObjectId("50bc89f788555f5ad35da8bb"), "id" : 2, "email" : "test2#test.com", "list" : [ "list1", "list2" ] }
Then by using the following code i am able to update document with id=1.
BasicDBObject cmd = new BasicDBObject().append("$push", new BasicDBObject("list", "list3"));
coll.update(new BasicDBObject().append("id", 1).append("email","test1#test.com"), cmd);
After the update rows look like :
{ "_id" : ObjectId("50bc89ef88555f5ad35da8ba"), "id" : 2, "email" : "test2#test.com", "list" : [ "list1", "list2" ] }
{ "_id" : ObjectId("50bc89f788555f5ad35da8bb"), "email" : "test1#test.com", "id" : 1, "list" : [ "list1", "list2", "list3" ] }
Check your code again. It should work with this code.
Related
In my Activity document I want to update a collection of actions status which ids are in a list :
{
"_id" : "...",
"actions" : [
{
"_id" : 1,
"status" : "todo"
},
{
"_id" : 2,
"status" : "in progress"
},
{
"_id" : 3,
"status" : "done"
},
{
"_id" : 4,
"status" : "done"
},
{
"_id" : 5,
"status" : "todo"
}
]
}
I tried to write a code using MongoOperation.updateMulti but it updates only one status at all :
mongoOperation.updateMulti(
new Query().addCriteria(
Criteria.where("_id").is(activityId).and("actionsActivite._id").in(actionsIds)),
new Update().set("actionsActivite.$.status", newStatut),
ActivityModel.class
);
I don't know where the problem is. Is my Query wrong ? My Update ?
I finally found the solution. I just add $[] operator in the update like this :
mongoOperation.updateMulti(
new Query().addCriteria(
Criteria.where("_id").is(activityId).and("actionsActivite._id").in(actionsIds)),
new Update().set("actionsActivite.$[].status", newStatut),
ActivityModel.class
);
I have these 2 documents in my collection:
{
"_id" : ObjectId("5722042f8648ba1d04c65dad"),
"companyId" : ObjectId("570269639caabe24e4e4043e"),
"applicationId" : ObjectId("5710e3994df37620e84808a8"),
"steps" : [
{
"id" : NumberLong(0),
"responsiveUser" : "57206f9362d0260fd0af59b6",
"stepOnRejection" : NumberLong(0),
"notification" : "test"
},
{
"id" : NumberLong(1),
"responsiveUser" : "57206fd562d0261034075f70",
"stepOnRejection" : NumberLong(1),
"notification" : "test1"
}
]
}
{
"_id" : ObjectId("5728f317a8f9ba14187b84f8"),
"companyId" : ObjectId("570269639caabe24e4e4043e"),
"applicationId" : ObjectId("5710e3994df37620e84808a8"),
"steps" : [
{
"id" : NumberLong(0),
"responsiveUser" : "57206f9362d0260fd0af59b6",
"stepOnRejection" : NumberLong(0),
"notification" : "erter"
},
{
"id" : NumberLong(1),
"responsiveUser" : "57206f9362d0260fd0af59b6",
"stepOnRejection" : NumberLong(1),
"notification" : "3232"
}
]
}
Now I'm trying to get the document with the max _id and the id that equals 0 from a document inside of the steps array. I also have a projection that is supposed to show only the id of the matched element and nothing else.
Here is my query:
collection
.find(new Document("companyId", companyId)
.append("applicationId", applicationId)
.append("steps",
new Document("$elemMatch",
new Document("id", 0))))
.sort(new Document("_id", 1))
.limit(1)
.projection(new Document("steps.id", 1)
.append("_id", 0));
And it returns:
Document{{steps=[Document{{id=0}}, Document{{id=1}}]}}
Why is it returning 2 documents instead of 1?
The result should be looking like:
Document{{id=0}}
What am I missing here? I know that is something basic, but I really can't spot my mistake here.
Your query document tells Mongo to return those documents where in the 'steps' array they have a document where id: 0. You are NOT telling Mongo to return ONLY that field. You can use $elemMatch inside the projection document to get what you want (I'm writing this in the Mongo shell syntax because I'm not too familiar with the Java syntax):
{ steps: { $elemMatch: { id: 0 } },
'steps.id': 1,
_id: 0
}
I've this document:
{
"_id" : ObjectId("54140782b6d2ca6018585093"),
"user_id" : ObjectId("53f4ae1ae750619418a20467"),
"date" : ISODate("2014-09-13T08:59:46.709Z"),
"type" : 0,
"tot" : 2,
"additional_info" : {
"item_id" : ObjectId("540986159ef9ebafd3dcb5d0"),
"shop_id" : ObjectId("53f4cc5a6e09f788a103d0a4"),
"ap_id" : ObjectId("53f4cc5a6e09f788a103d0a5")
},
"transactions" : [
{
"_id" : ObjectId("54140782b6d2ca6018585091"),
"date_creation" : ISODate("2014-09-13T08:59:46.711Z"),
"type" : -1
},
{
"_id" : ObjectId("54140782b6d2ca6018585092"),
"date_creation" : ISODate("2014-09-13T08:59:46.788Z"),
"type" : 1
}
]
}
and I need to add 2 more field to the first transaction opbject:
- date_execution: date
- result: this bson document
{ "server_used" : "xxx.xxx.xxx.xxx:27017" , "ok" : 1 , "n" : 1 , "updated_executed" : true} (m_OR.getDocument() in the following code example)
to obtaing that document
{
"_id" : ObjectId("54140811b6d25137753c1a1a"),
"user_id" : ObjectId("53f4ae1ae750619418a20467"),
"date" : ISODate("2014-09-13T09:02:09.098Z"),
"type" : 0,
"tot" : 2,
"additional_info" : {
"item_id" : ObjectId("540986159ef9ebafd3dcb5d0"),
"shop_id" : ObjectId("53f4cc5a6e09f788a103d0a4"),
"ap_id" : ObjectId("53f4cc5a6e09f788a103d0a5")
},
"transactions" : [
{
"_id" : ObjectId("54140811b6d25137753c1a18"),
"date_creation" : ISODate("2014-09-13T09:02:09.100Z"),
"type" : -1,
"result" : {
"server_used" : "xxx.xxx.xxx.xxx:27017",
"ok" : 1,
"n" : 1,
"updated_executed" : true
},
"date_execution" : ISODate("2014-09-13T09:02:15.370Z")
},
{
"_id" : ObjectId("54140811b6d25137753c1a19"),
"date_creation" : ISODate("2014-09-13T09:02:09.179Z"),
"type" : 1
}
]
}
The only way I was able to do that is the do 2 separates updates (update is a my wrapper funciont that execute the real updates in mongodb and it works fine):
// where
BasicDBObject query = new BasicDBObject();
query.append("transactions._id", m_Task.ID());
// new value for result - 1st upd
BasicDBObject value = new BasicDBObject();
value.put("$set",new BasicDBObject("transactions.$.date_execution",new Date()));
update(this._systemDB, "activities", query, value);
// new value for date_execution - 2nd upd
value = new BasicDBObject();
value.put("$set",new BasicDBObject("transactions.$.result",m_OR.getDocument()));
update(this._systemDB, "activities", query, value);
If I try to do this:
BasicDBObject value = new BasicDBObject();
value.put("$set",new BasicDBObject("transactions.$.date_execution",new Date()));
value.put("$set",new BasicDBObject("transactions.$.result",m_OR.getDocument()));
or = update(this._systemDB, "activities", query, value);
just the 2nd set will be applied.
Is there any way do avoid the double execution and apply the update with just one call?
Basic rule of "hash/map" objects is that you can only have one key. It's the "highlander" rule ( "There can be only one" ) applied in general reason. So just apply differently:
BasicDBObject value = new BasicDBObject();
value.put("$set",
new BasicDBObject("transactions.$.date_execution",new Date())
.add( new BasicDBObject("transactions.$.result",m_OR.getDocument() )
);
So basically "both" field arguments are part of the "$set" statement as in the serialized form:
{
"$set": {
"transactions.$.date_execution": new Date(),
"transactions.$.result": m_Or.getDocument()
}
}
Which is basically what you want in the end.
Your suggestion was right, just had to fix a little the syntax this way:
BasicDBObject value = new BasicDBObject();
value.put("$set",
new BasicDBObject("transactions.$.date_execution",new Date())
.append("transactions.$.result",m_OR.getDocument())
);
This worked perfectly ;)
Thanks!
Samuel
This is the object in the database
{
"_id" : { "$oid" : "53a9ce071e24a7a0a4bef03a"} ,
"name" : "name4" ,
"sections" : [
{
"id" : "sectionId1" ,
"subs" : [
{ "name" : "name1" , "enable" : true} ,
{ "name" : "name2" , "enable" : false} ,
{ "name" : "name3" , "enable" : true}
]
},
{
"id" : "sectionId2",
"subs" : [
{ "name" : "name1" , "enable" : true} ,
{ "name" : "name5" , "enable" : false} ,
{ "name" : "name6" , "enable" : true}
]
},
{ "id" : "sectionId3"}
]
}
and this is my code :
BasicDBObject query = new BasicDBObject();
query.append("name", "name4");
query.append("sections", new BasicDBObject(
"$elemMatch", new BasicDBObject("id", "sectionId2")
));
query.append("sections.subs", new BasicDBObject(
"$elemMatch", new BasicDBObject("name", "name1")
));
I am trying to access the 'name1' in 'subs' of 'sectionId2'. But my query returns the sub in 'sectionId1'. I am having this problem only for 'name1'. I can access 'name2', 'name3' etc without any errors because they are unique.
Thanks in advance!
It may be because of the $elemMatch that you are using, the $elemMatch projection returns only the first matching element from the array. see it here
I have
{
"Districts" :
[{ "name" : "Krishna"
, "Locations" : [{ "name" : "Vijayawada"}
,{ "name" : "Machilipatnam"}]}
, { "name" : "Guntur"
, "Locations" : [{ "name" : "Satenpalli"}]}
]
, "_id" : 1
, "name" : "Andhra Pradesh"
}
I am trying to create one more Location "Achampet" if District name is "Guntur" so the result should be this below. The result should be the same even if I try to add Achampet more than once.
{
"Districts" :
[{ "name" : "Krishna"
, "Locations" : [{ "name" : "Vijayawada"}
,{ "name" : "Machilipatnam"}]}
, { "name" : "Guntur"
, "Locations" : [{ "name" : "Satenpalli"}
,{ "name" : "Achampet"}]}
]
, "_id" : 1
, "name" : "Andhra Pradesh"
}
But my java code doesn't work
DBObject newLoc = new BasicDBObject("Districts", new BasicDBObject("name", distName).append("Locations", new BasicDBObject("name", locName)));
if (statesColl.findOne(newLoc) == null) {
DBObject updateLoc = new BasicDBObject("$push", newLoc);
statesColl.update(queryDist, updateLoc);
}
It is creating a new District everytime I try to add a location. How can I fix this?
This is how you can do it using the $ positional operator in Java:
...
DBObject selectQuery = new BasicDBObject("_id", 1); // Matches the document
selectQuery.append("Districts.name", distName); // Matches the element in the array where District name = Guntur
BasicDBObject updateFields = new BasicDBObject();
updateFields.put("Districts.$.Locations", new BasicDBObject("name":"Achampet"));
DBObject updateQuery = new BasicDBObject("$addToSet", updateFields);
statesColl.update(selectQuery, updateQuery);
...