Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

update query in mongodb?

 
Joseph Hwang
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 255
2
IntelliJ IDE Java Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 255
2
IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic