aspose file tools*
The moose likes MongoDB and the fly likes update query in mongodb? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » MongoDB
Bookmark "update query in mongodb?" Watch "update query in mongodb?" New topic
Author

update query in mongodb?

Joseph Hwang
Greenhorn

Joined: Aug 17, 2013
Posts: 16
Belows are my document architecture.

"_id" : ObjectId("529806ace4b054419e5932a0"),
"carrierCode" : "AA",
"carrierName" : "American Airlines Inc.",
"delayTime" : {
"arrival" : {
"date" : "198710",
"times" : 26072
},
"arrival" : {
"date" : "198711",
"times" : 26485
}
}

I want to update like this,

"_id" : ObjectId("529806ace4b054419e5932a0"),
"carrierCode" : "AA",
"carrierName" : "American Airlines Inc.",
"delayTime" : {
"arrival" : {
"date" : "198710",
"times" : 26072
},
"arrival" : {
"date" : "198711",
"times" : 26485
},
"arrival" : {
"date" : "total",
"times" : 52557
}
}

But I have no idea what the query has to be.

BasicBSONObject query = new BasicBSONObject("carrierCode", "AA");
query.append("carrierName", "American Airlines Inc.");
query.append("delayTime", "arrival");

BasicBSONOBject update = new BasicBSONObject("$pushAll", new BasicBSONObject("date", "total").append("times", new Integer(52557)));

collection.update(query, update, true, false);

update command does not work. Any help?
Thanks in advance.
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

The code may look like this

However
1) should use $push instead of $pushAll as $pushAll is deprecated
2) delayTime should be array instead of document
3) query using carrierCode should be sufficient I guess ?
4) date and times should use Date instead for sorting or querying in the future

5) should not update document like that, instead your query should use aggregation. Otherwise, total need to be updated each time
Joseph Hwang
Greenhorn

Joined: Aug 17, 2013
Posts: 16
Thanks for your reply! How about this case? My doc format uses com.mongdb.BasicDBList

"_id" : ObjectId("52a2fc95e4b082eedf558be4"),
"carrierCode" : "AA",
"carrierName" : "American Airlines Inc.",
"delayTime" : {
"departure" : [
{ "date" : ISODate("1987-09-30T14:00:00Z"), "times" : 26795 },
{ "date" : ISODate("1987-10-31T15:00:00Z"), "times" : 26005 },
{ "date" : ISODate("1987-11-30T15:00:00Z"), "times" : 30507 },
.....
],
"arrival" : [
{ "date" : ISODate("1987-11-30T15:00:00Z"), "times" : 30864 },
{ "date" : ISODate("1987-12-31T15:00:00Z"), "times" : 27350 },
{ "date" : ISODate("1988-01-31T15:00:00Z"), "times" : 24986 },
....
]
}

I want to update like below ;

"_id" : ObjectId("52a2fc95e4b082eedf558be4"),
"carrierCode" : "AA",
"carrierName" : "American Airlines Inc.",
"delayTime" : {
"departure" : [
{ "date" : ISODate("1987-09-30T14:00:00Z"), "times" : 26795 },
{ "date" : ISODate("1987-10-31T15:00:00Z"), "times" : 26005 },
{ "date" : ISODate("1987-11-30T15:00:00Z"), "times" : 30507 },
.....
{ "date" : "total" , "times" : 777777}
],
"arrival" : [
{ "date" : ISODate("1987-11-30T15:00:00Z"), "times" : 30864 },
{ "date" : ISODate("1987-12-31T15:00:00Z"), "times" : 27350 },
{ "date" : ISODate("1988-01-31T15:00:00Z"), "times" : 24986 },
....
{ "date" : "total" , "times" : 999999}
]
}

I codes modifier like your codes

BasicBSONObject dateAndTimes = new BasicBSONObject("date", "total").append("times", 7777);
BasicBSONObject departure = new BasicBSONObject("departure", dateAndTimes);
BasicDBObject modifier = new BasicDBObject("$push", new BasicBSONObject("delayTime", departure));

Then it throws Cannot apply $push/$pushAll modifier to non-array message

Then please inform me how the query has to be! Your help will be appreciated. Thanks
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

departure and arrival inside delayTime
so your code may look like

I haven't tried and again I think using aggregation to calculate total is better than pre-calculate.
Vishal Baid
Ranch Hand

Joined: Jul 18, 2012
Posts: 42
    
    1
Hi,
The query for the update will look like this :
db.rec.update({"carrierCode":"AA"},{$push:{"delayTime":{"arrival":{"date":"19811","times":5257}}}})

Before you run this query, I want to inform you delayTime should be array not collection.

Please guys, If I am wrong, please let me know.
This will help in improving myself.
Suggestions most welcome.

Regards,
Vishal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: update query in mongodb?