Update value mongo using Java - java

I am using Java Mongo driver for the DB interaction. I have regular updates to be performed on the DB rows and the object that is quite nested. Something like this :
MyObject:
{
_id: dbGeneratedId,
myId: "A String ID that i created",
myTime: "new Date()",
myList:
[
{
myString: "abcdefghij",
myInteger: 9000
},
{
myString: "qwertyasdf",
myInteger: 9001
},
{
myString: "loremipsum",
myInteger: 9002
}
]
}
Each update involves either adding a new List item under myList or appending some string to the myString object in each of the List item. I found a lot of references for writing/finding items and none for updating items in a nested object. Can someone help me with this.
Edit 1: It would also be helpful if someone points out how to get one of the List items based on a myInteger search
PS: new to mongo thro Java, excuse my ignorance

You can insert new list item using the $push operator.
You can run the following command on the shell to add new list item.
db.myObject.update({"myId" : "A String ID that i created"},{$push:{myList: {myString:"new string", myInteger:9003}}})
You can add list item using Java Driver as follows.
DBCollection coll = db.getCollection("myObject");
DBObject query = new BasicDBObject("myId", "A String ID that i created");
DBObject listItem = new BasicDBObject();
listItem.put("myString", "my new string");
listItem.put("myInteger", 9003);
DBObject updateObj = new BasicDBObject("myList", listItem);
coll.update(query, new BasicDBObject("$push", updateObj));
You can get single element as follows on the shell.
db.myObject.find({"myList.myInteger" : 9003}, {"myList.$" : 1})
From Java Driver you can run same code as follows :
DBCursor cur = coll.find(new BasicDBObject("myList.myInteger", 9003), new BasicDBObject("myList.$", 1));
You can remove object as follows :
db.nested.update({"myId" : "A String ID that i created"},{$pull:{myList: {myString:"new string", myInteger:9003}}})
In Java you can do it as follows :
DBCollection coll = db.getCollection("myObject");
DBObject query = new BasicDBObject("myId", "A String ID that i created");
DBObject listItem = new BasicDBObject();
listItem.put("myString", "my new string");
listItem.put("myInteger", 9003);
DBObject updateObj = new BasicDBObject("myList", listItem);
coll.update(query, new BasicDBObject("$pull", updateObj));
PS : Currently it is not possible to update all items in an array. There is an open issue on the Jira. You can check it from JIRA-1243

Related

Save Mongodb aggregation results on multiple id field in a new collection

I have to aggregate some data in mongodb through the java driver, and save the aggregation result in a new collection, and later print this collection in a Jtable; the id field can be not simple in my case (i can't know if it is a single value or not) so I followed this:
How to write multiple group by id fields in Mongodb java driver
when I want to save the result in a new collection, doing this:
for (DBObject result : output.results()) {
report.insert(result);
}
i have this error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: fields stored in the db can't have . in them. (Bad Key: 'store.address.store_state')
this is a piece of the collection where I do the aggregation:
{
"_id" : ObjectId("549ef5d17c8db9efa75d49c9"),
"store" : {
"store_id" : NumberLong(2),
"store_type" : "Small Grocery",
"region_id" : NumberLong(78),
"store_name" : "Store 2",
"store_number" : NumberLong(2),
"address" : {
"store_street_address" : "5203 Catanzaro Way",
"store_city" : "Bellingham",
"store_state" : "WA",
"store_postal_code" : "55555",
"store_country" : "USA",
"store_manager" : "Smith"
},
.....}
Can anyone help me?
Note: ID Field can be simple (only a field) or complex (two or more fields)
The new collection I want to create must have all the fields that compose the ID in the aggregation, and all the fields result by aggregation. That's beacause I had to print that collection in this way: Retrieve data from MongoDB collection into Swing JTable
for example:
DBObject fields = new BasicDBObject("unit_sales", 1);
fields.put("store.store_type", 1);
fields.put("store.store_name", 1);
fields.put("store.address.store_city", 1);
fields.put("_id", 0);
DBObject project = new BasicDBObject("$project", fields );
Map<String, Object> dbObjIdMap = new HashMap<String, Object>();
dbObjIdMap.put("store_type", "$store.store_type");
dbObjIdMap.put("store_name", "$store.store_name");
dbObjIdMap.put("store_city", "$store.address.store_city");
DBObject groupFields = new BasicDBObject( "_id", dbObjIdMap);
groupFields.put("average", new BasicDBObject("$avg", "$unit_sales"));
DBObject group = new BasicDBObject("$group", groupFields);
AggregationOutput output = collection.aggregate(project, group);
So i want as result a table with column store_type,store_name, store_city, average.
I know I'm exposing my problem here so bad, but i'm not english and i don't speak very well the language, so please try to understand me.. sorry

Add new data to existing collection in mongo in java

I need two to add new items to the existing data in mongo db.
This is mongo db I have the following data.
{
"_id" : ObjectId("53ce11e7d0881d32d9fa935f"),
"name" : "massive riots",
"lastFeachedTime" : "Jul 15, 2014 12:55:27 PM"
}
Here I have to find the data based on name and the I have to add another two items two it.
Here is my code.
DBObject queryObject = new BasicDBObject().append("name", keyword);
if (null == newFetchTime) {
}
DBObject updateObject = new BasicDBObject();
updateObject.put("nextPageToken", nextPageToken);
updateObject.put("prevPageToken", prevPageToken);
Utils utils = new Utils();
DBCollection collection = utils.getStaging().getCollection("test");
collection.update(queryObject, updateObject, true, false);
But I am do update the existing value get removed and the new data get added.
Can any one tell me how to add the items to the existing data in mongo db.
You want the $set operator in your update. This allows the specified fields to be altered without affecting any of the existing fields in the document, unless the specified field exists in which case that field is overwritten:
DBObject update = new BasicDBObject(
"$set", new BasicDBObject()
.append("nextPageToken",nextPageToken)
.append("prevPageToken",prevPageToken)
);
Works out to the equivalent in shell:
{ "$set" : { "nextPageToken" : nextPageToken , "prevPageToken" : prevPageToken }}

get some attributes from mongodb except one or two

I would like to get some information which is in a mongoDB except some attributes.
I tried it in cmd and it worked:
db.orders.find({name:"chabeee"},{_id:0, name:1, worksAt:1})
Then I get this result:
{ "name" : "chabeee", "worksAt" : "jobAtBp" }
{ "name" : "chabeee", "worksAt" : "jobAtRE" }
Its okay, but I want to get in a Java Program. How can I do that?
You have to create one additional BasicDBObject, which will be used for pointing out which exact keys to be fetched. And finally the DBCollection#find(DBObject ref, DBObject keys) method has to be invoked in order to pass the desired projection keys.
BasicDBObject query = new BasicDBObject("name", "chabeee");
BasicDBObject keys = new BasicDBObject();
keys.put("_id", 0);
keys.put("name", 1);
keys.put("worksAt", 1);
BasicDBCursor result = collection.find(query, keys);
Then you just have to iterate over the BasicDBCursor and verify the result.
while (cursor.hasNext()) {
System.out.println(cursor.next());
}

Query a document on array elements in MongoDB using Java

I am new to MongoDB. My sample document is
{
"Notification" : [
{
"date_from" : ISODate("2013-07-08T18:30:00Z"),
"date_too" : ISODate("2013-07-30T18:30:00Z"),
"description" : "fdfd",
"url" : "www.adf.com"
},
{
"date_from" : ISODate("2013-07-01T18:30:00Z"),
"date_too" : ISODate("2013-07-30T18:30:00Z"),
"description" : "ddddddddddd",
"url" : "www.pqr.com"
}
],
I am trying to update the Notification whose "url" : "www.adf.com". My Java code to do this is:
BasicDBObject query=new BasicDBObject("url","www.adf.com");
DBCursor f = con.coll.find(query);
It does not search for the document whose "url" is "www.adf.com".
You have a nested document in this case. Your document has a field Notification which is an array storing multiple sub-objects with the field url. To search in a sub-field, you need to use the dot-syntax:
BasicDBObject query=new BasicDBObject("Notification.url","www.adf.com");
This will, however, return the whole document with the whole Notification array. You likely only want the sub-document. To filter this, you need to use the two-argument version of Collection.find.
BasicDBObject query=new BasicDBObject("Notification.url","www.example.com");
BasicDBObject fields=new BasicDBObject("Notification.$", 1);
DBCursor f = con.coll.find(query, fields);
The .$ means "only the first entry of this array which is matched by the find-operator"
This should still return one document with a sub-array Notifications, but this array should only contain the entry where url == "www.example.com".
To traverse this document with Java, do this:
BasicDBList notifications = (BasicDBList) f.next().get("Notification");
BasicDBObject notification = (BasicDBObject) notifications.get(0);
String url = notification.get("url");
By the way: When your database grows you will likely run into performance problems, unless you create an index to speed up this query:
con.coll.ensureIndex(new BasicDBObject("Notification.url", 1));

How to update value of specific embedded document, inside an array, of a specific document in MongoDB?

I have the following structure in my document:
{
_id : ObjectId("43jh4j343j4j"),
array : [
{
_arrayId : ObjectId("dsd87dsa9d87s9d7"),
someField : "something",
someField2 : "something2"
},
{
_arrayId : ObjectId("sds9a0d9da0d9sa0"),
someField : "somethingElse",
someField2 : "somethingElse2"
}
]
}
I want to update someField and someField2 but only for one of the items in the array, the one that matches _arrayId (e.g. _arrayId : ObjectId("dsd87dsa9d87s9d7"); and only for this document (e.g. _id : ObjectId("43jh4j343j4j") ) and no other.
The arrayIds are not unique to the document that's why I need it to be for a specific document. I could use the $ positional operator if I wanted to update that value within the array for every document it exists in, but that's not what I want.
I am trying to accomplish this in java but a command line solution would work as well.
Here is RameshVel's solution translated to java:
DB db = conn.getDB( "yourDB" );
DBCollection coll = db.getCollection( "yourCollection" );
ObjectId _id = new ObjectId("4e71b07ff391f2b283be2f95");
ObjectId arrayId = new ObjectId("4e639a918dca838d4575979c");
BasicDBObject query = new BasicDBObject();
query.put("_id", _id);
query.put("array._arrayId", arrayId);
BasicDBObject data = new BasicDBObject();
data.put("array.$.someField", "updated");
BasicDBObject command = new BasicDBObject();
command.put("$set", data);
coll.update(query, command);
You could still use $ positional operator to accomplish this. But you need to specify the objectid of the parent doc along with the _arrayid filter. The below command line query works fine
db.so.update({_id:ObjectId("4e719eb07f1d878c5cf7333c"),
"array._arrayId":ObjectId("dsd87dsa9d87s9d7")},
{$set:{"array.$.someField":"updated"}})
...and this is how to do it with mongo-driver version >= 3.1 (mine is 3.2.2):
final MongoClient mongoClient = new MongoClient(new MongoClientURI(mongoURIString));
final MongoDatabase blogDatabase = mongoClient.getDatabase("yourDB");
MongoCollection<Document> postsCollection = blogDatabase.getCollection("yourCollection");
ObjectId _id = new ObjectId("4e71b07ff391f2b283be2f95");
ObjectId arrayId = new ObjectId("4e639a918dca838d4575979c");
Bson filter = Filters.and(Filters.eq( "_id", id ), Filters.eq("array._arrayId", arrayId));
Bson setUpdate = Updates.set("array.$.someField", "updated");
postsCollection.updateOne(postFilter, setUpdate);
Seeing as none of the answers actually explain how to do this a) in Java and b) for multiple fields in a nested array item, here is the solution for mongo-java-driver 3.12.3.
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import org.bson.Document;
import org.bson.types.ObjectId;
MongoClient mongoClient = MongoClients.create(...);
MongoDatabase db = mongoClient.getDatabase("testDb");
MongoCollection<Document> collection = db.getCollection("testCollection");
collection.updateOne(
Filters.and(
Filters.eq("_id", new ObjectId("43jh4j343j4j")),
Filters.eq("array._arrayId", new ObjectId("dsd87dsa9d87s9d7"))
),
Updates.combine(
Updates.set("array.$.someField", "new value 1"),
Updates.set("array.$.someField2", "new value 2")
)
);
This thread has helped me towards the right solution, but I had to do more research for the full solution, so hoping that someone else will benefit from my answer too.

Categories