Update a single field to array in mongodb using JAVA - arrays

I have documents of below format:
{
"_id" : ObjectId("559bae5df3fcf46e468a9779"),
"CPF" : "129-87-716",
"NAME" : "Donald Carver",
"CONTACT" : 1827626847,
"ADDRESS" : "937 Hamburg Path",
"EMAIL_ADDRESS" : "constpherd#yah00.net",
"CARDS" : {
"CREDIT_CARD_NUMBER" : "1678-768-420-4188",
"CARD_TYPE" : "IVISA",
"EXPIRY_DATE" : "Mon May 21 15:23:53 IST 2018"
}
}
I want to update an array of document .
"TRANSACTIONS":[{"TRANSACTION_NUMBER":"1","TRANSACTION_DATE":"07/07/2015","BANK_ID":"200","AMOUNT":100},
{"TRANSACTION_NUMBER":"2","TRANSACTION_DATE":"08/07/2015","BANK_ID":"0","AMOUNT":200},
{"TRANSACTION_NUMBER":"3","TRANSACTION_DATE":"06/07/2015","BANK_ID":"200","AMOUNT":100}
]
}
And it should look like below:
{
"_id" : ObjectId("559bae5df3fcf46e468a9779"),
"CPF" : "129-87-716",
"NAME" : "Donald Carver",
"CONTACT" : 1827626847,
"ADDRESS" : "937 Hamburg Path",
"EMAIL_ADDRESS" : "constpherd#yah00.net",
"CARDS" : {
"CREDIT_CARD_NUMBER" : "1678-768-420-4188",
"CARD_TYPE" : "IVISA",
"EXPIRY_DATE" : "Mon May 21 15:23:53 IST 2018"
}
"TRANSACTIONS":[{"TRANSACTION_NUMBER":"1","TRANSACTION_DATE":"07/07/2015","BANK_ID":"200","AMOUNT":100},
{"TRANSACTION_NUMBER":"2","TRANSACTION_DATE":"08/07/2015","BANK_ID":"0","AMOUNT":200},
{"TRANSACTION_NUMBER":"3","TRANSACTION_DATE":"06/07/2015","BANK_ID":"200","AMOUNT":100}
]
}
}
This is what I am trying to do it using java:
String[] creditcard = line.split(cvsSplitBy);
DBObject listItem = new BasicDBObject("TRANSACTION", new BasicDBObject("TRANSACTION_NUMBER",creditcard[1])
.append("TRANSACTION_DATE",creditcard[2])
.append("TRANSACTION_TYPE",creditcard[3])
.append("BANK_ID",creditcard[4])
.append("AMOUNT",creditcard[5])
);
DBObject updateQuery = new BasicDBObject("$push", listItem);
userimage.update(new BasicDBObject().append("CARDS.CREDIT_CARD_NUMBER", creditcard[0]), updateQuery);
The program updates one row when I run for the first time.When I run second time the program does not insert any doc into the array ...
I am not able to find out the logical error in the program..

Related

How to lookup in another collection with objectID

collection 'bookborrow'
"_id" : ObjectId("62ebbd1f4d6ab77fe3dad494"),
"userId" : "62ea90c6691228b6157cc0f9",
"data" : {
"todayDate" : "04/08/2022",
"dateofreturn" : "19/08/2022",
"bookid" : "62e53c1ff5d4c45fb7853d9e"
}
}
collection 'bookdetails'
{
"_id" : ObjectId("62e53c1ff5d4c45fb7853d9e"),
"bookname" : "How To Own Your Own Mind",
"bookauthor" : "Napoleon Hill",
"counterbooks" : "2"
}
output look like
"bookname" : "How To Own Your Own Mind",
"todayDate" : "04/08/2022",
"dateofreturn" : "19/08/2022",
bookid is uniq id .it's bookdetails match _id

Arabic text in swift

i have array of objects like this
"sub-specialty" : [
{
"id" : 1,
"title" : "Adult Physiotherapy",
"ar_title" : "علاج طبيعي بالغين",
"name" : "علاج طبيعي بالغين"
},
{
"id" : 2,
"title" : "Pediatric Physiotherapy",
"ar_title" : "علاج طبيعي اطفال",
"name" : "علاج طبيعي اطفال"
},
{
"id" : 3,
"title" : "Sport Injuries",
"ar_title" : "اصابات ملاعب",
"name" : "اصابات ملاعب"
},
{
"id" : 4,
"title" : "Rehabilitation",
"ar_title" : "تأهيل",
"name" : "تأهيل"
}
]
when i use map to grab name only like this
let sub = sub-specialty.map({($0.name ?? "")}).joined(separator: ", ") ?? ""
i got rubbish characters or text is flipped please see the attached image
enter image description here
You need to get the text as String and set wherever you want to set. It will work fine. I have worked with multi languages and this work for me.

Find element inside array and return only array

I'm new to MongoDB, so i'm having some newbie problems.
I have this document on DB
{
"_id" : ObjectId("5a7ba73dbf27878474ac7682"),
"Enterprise" : "SpaceX",
"Address" : "qwerty",
"users" : [
{
"name" : "Elon",
"number" : "123456",
"email" : "elon#mars.com",
"user" : "elon",
"pass" : "byeroadster"
},
{
"name" : "Larry",
"number" : "3215465",
"email" : "larry#google.com",
"user" : "larry",
"pass" : "googlepassword"
}
]
}
And i'm doing this query
var db = client.db('hauz_manager');
var findUser = db.collection('system_enterprise');
findUser.find({'users.user': data.user},{contatos: 1}).toArray(function(err, result){
console.log(result.users);
});
With that, i'm having like
Enterprise: "SpaceX"
...
users: [{Object}]
And, i'm not able to do some result.users, returns undefined.
tnx.
console.log(result.users)
I think your callback return array.
You should check callback.
Do you need a loop?

How to retrieve data from mongodb using scala

I have connected to mongodb and filter data like this:
val coll = mongoDB.getTable(db, "report")
val query = new BasicDBObject()
query.put("version",getVersion)
val fields = new BasicDBObject()
fields.put("version",1)
fields.put("project",1)
fields.put("uri",1)
fields.put("test",1)
fields.put("browser",1)
val cursor = coll.find(queryObject,fields)
then get a document like this:
{
"_id" : ObjectId("5a968c6cffac6135b09d6cd5"),
"project" : "com.sink.www.helper",
"uri" : "com.sink.www.helper.somketest",
"browser" : "firefox",
"develop" : "scala",
"duration" : "0.61",
"test" : "acceptance"
"version" : "1.0"
"try" : 1
}
{
"_id" : ObjectId("5a968daaffacc7195c2f9b6i"),
"project" : "com.sink.www.helper",
"uri" : "com.sink.www.helper.somketest",
"browser" : "firefox",
"develop" : "scala",
"duration" : "0.62",
"test" : "acceptance"
"version" : "1.0"
"try" : 1
}
{
"_id" : ObjectId("5a968ea5fface1a723ffr246"),
"project" : "com.sink.www.helper",
"uri" : "com.sink.www.helper.somketest",
"browser" : "chrome",
"develop" : "scala",
"duration" : "0.58",
"test" : "acceptance"
"version" : "1.0"
"try" : 1
}
.....
My question is when "version" is specific value like '1.0', then the field try=try + 1, I have write code like this but this will read mongodb every time, this will cost much time:
while (cursor.hasNext())
{
val details = cursor.next()
if (details.get("version").toString() == getVersion && details.get("browser").toString() == getBrowser)
{
try += 1
}
}
because there are much data in mongodb, so it's not adapt to our project. so I want to get specific data to array or others and get to judge then let try+1, I write code like this but report error:
val scenarioArray = coll.find(queryObject,fields).toArray
for (i <- 0 to scenarioArray.length - 1)
{
println(scenarioArray(i))
}
error: value length is not a member of java.util.List[com.mongodb.DBObject]
error: java.util.List[com.mongodb.DBObject] does not take parameters
I don't know how to retrieve the document to array or list or others container, could anyone get to help on this? Thanks.

update nth document in a nested array document in mongodb

I need to update a document in an array inside another document in Mongo DB
{
"_id" : ObjectId("51cff693d342704b5047e6d8"),
"author" : "test",
"body" : "sdfkj dsfhk asdfjad ",
"comments" : [
{
"author" : "test",
"body" : "sdfkjdj\r\nasdjgkfdfj",
"email" : "test#tes.com"
},
{
"author" : "hola",
"body" : "sdfl\r\nhola \r\nwork here"
}
],
"date" : ISODate("2013-06-30T09:12:51.629Z"),
"permalink" : "jaiho",
"tags" : [
"jaiho"
],
"title" : "JAiHo"
}
Q1) Update email of 0th element of comments array
db.posts.update({"permalink" : "haha"},{$set:{"comments.0.email":1}})
This doesn't throw any exception but doesn't update anything as well
Q2) Add a field on nth element of comments array number_likes
db.posts.update({"permalink" : "haha"},{$inc:{"comments.0.num_likes":1}})
Doesn't work either.
Am I missing something here?
Q1: If you update with permalink 'jaiho' instead of 'haha', it most certainly updates the email;
> db.posts.update({"permalink" : "jaiho"},{$set:{"comments.0.email":1}})
> db.posts.find()
..., "email" : 1 },...
Q2: Same goes for this include;
> db.posts.update({"permalink" : "jaiho"},{$inc:{"comments.0.num_likes":1}})
> db.posts.find()
..., "num_likes" : 1 },...
If you are trying to do it dynamically in Node JS following should work.
i = 0;
selector = {};
operator = {};
selector['comments.' + i + '.email'] = 1; // {'comments.0.num_likes' : 1}
operator['$inc'] = selector; // {'$inc' : {'comments.0.num_likes' : 1} }
db.posts.update({'permalink' : 'xyz'}, operator);

Resources