mongodb updateMany nested array - arrays

I've got the following object and want to update
ArrayOne.ArrayTwo.DocumentTypeID from that ID to a different ID:
{
"_id" : NUUID("97cd06e9-3449-45db-be69-fce30ee1f9b8"),
"ArrayOne" : [
{
"ID" : NUUID("70d99c86-414c-401a-9cc9-91661806c10f"),
"ArrayTwo" : [
{
"EvidenceID" : NUUID("0799492a-b312-4832-a830-ce4ff841d5fd"),
"DocumentTypeID" : NUUID("fa67acee-2d56-4174-954f-f1bdc892add0"),
"FileID" : NUUID("e5f433c6-8c7f-008d-6d2a-12a908f91690"),
"FileName" : "testqwerty.pdf",
"AdjudicationStatus" : 0,
"SubmitDateTime" : ISODate("2022-09-12T14:14:15.060Z")
}
],
"Propertyone" : null,
"Propertytwo" : null,
"Propertythree" : 0
}
]
}
Trying to use:
db.getCollection('Collection').updateMany
(
{"ArrayOne.ArrayTwo.DocumentTypeID" : NUUID("FA67ACEE-2D56-4174-954F-F1BDC892ADD0")},
{$set : {"ArrayOne.ArrayTwo.DocumentTypeID" : NUUID("090F89EF-3AAE-45F2-853F-5B8C7B8AC0CA")}}
)
I do not want to specify the element as 'ArrayOne' can have up to 10 elements and so can "ArrayTwo" the DocumentTypeID however has a unique GUID.
Can someone please let me know how I can do this ?
Thanks

Related

[MongoDB]How to find an object from array?

I'm trying to query single object from an array of objects.
The array looks like this.
db.getCollection('user').getIndexes();
[
{
"v" : 1,
"key" : {
"_id" : 1
}
},
{
"v" : 1,
"key" : {
"name" : 1
}
},
{
"v" : 1,
"key" : {
"admin" : 1
}
}
]
Don't know how to find an element with "_id" property name in "key" object.
db.getCollection('user').getIndexes().[some sorting query];
{
"v" : 1,
"key" : {
"_id" : 1
}
}
Does anyone have an idea? Thank you.
The getIndexes helper in the mongo shell accepts a filter as an argument, like:
db.getCollection("user").getIndexes({"key._id":{$exists:true}})
From a driver, you can run the listIndexes command similarly:
database.runCommand({listIndexes:"user", "key._id":{$exists:true}})

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.

MongoDB month query on array records

I have one json object in mongodb which looks like this.
JSON Object :
{
"_id" : ObjectId("58736cfc7d43c305461cdba7"),
"pb_event" : [
{
"event_type" : "Birthday",
"event_date" : ISODate("2016-05-09T13:01:11.967+0000")
},
{
"event_type" : "Aniversary",
"event_date" : ISODate("2016-06-09T13:01:11.967+0000")
}
]
}
When i try below query :
db.temp.aggregate({$project : {month : {$month : "$pb_event.event_date"}}} )
It gives me error like this :
"Can't Convert from BSON type array To date"

how to do custom filter in angular JS and what should be the return type

I am using MEAN stack and trying to filter the data from the ng-repeat.
Here goes my HTML code
<section data-ng-controller="BookingsController" data-ng-init="find()">
<div data-ng-repeat="x in bookings|filter:select_booking" data-ng-href="#!/bookings/{{booking._id}}" class="list-group-item">
<label>Booking ID :</label>{{x.booking_id}}
<label>Membership ID :</label>{{x.membership_id}}
<label>Check Out Date :</label>{{ x.checkout_time | date : "longDate" }}
<label>Rooms :</label><span ng-repeat="room in booking.room_id">{{room}} </span>
<button class="pull-right" value="Block" ng-click="checkout(x)">Check Out</button>
<button class="pull-right" style="margin-right:10px" value="Block" ng-click="services(x)">Add Services</button>
<br>
</div>
</section>
My filter name in the above code is select_booking.
The code for ng-controller goes here
$scope.checkindetails = CheckinDetails.query();/*accessing the mongo db values*/
$scope.select_booking = function (a) {
var f = 0;
jQuery.each($scope.checkindetails, function (key, val) {
if (val.booking_id == a.booking_id) {
console.log("in if");
console.log(a.booking_id);
return true;
}
});
};
My Console.log result:
bookings.client.controller.js:300in if
bookings.client.controller.js:301 ASHISH_BOOK_002
bookings.client.controller.js:300 in if
bookings.client.controller.js:301 ASHISH_BOOK_000
bookings db:here referred as a
{
"_id" : ObjectId("54f02f7ba3c39d8c106bd3f7"),
"user" : ObjectId("54eede3363b3e60c2133fe06"),
"created" : ISODate("2015-02-27T08:48:59.890Z"),
"date_of_cancellation" : "",
"cancelled" : "No",
"points" : 0,
"booking_id" : "ASHISH_BOOK_000",
"checked_out" : "Yes",
"booking_date" : ISODate("2015-02-27T08:48:59.890Z"
"adult_count" : 4,
"room_id" : [
"101",
"102"
],
"rooms_count" : 2,
"resort_id" : "ASH_RESORT001",
"checkout_time" : "2015-02-28T18:30:00.000Z",
"checkin_time" : "2015-02-26T18:30:00.000Z",
"guest" : "no",
"membership_id" : "4",
"__v" : 0
}
{
"_id" : ObjectId("54f560e2e0d3bed8069214f5"),
"user" : ObjectId("54ec4eb13d50decc1f5307b4"),
"created" : ISODate("2015-03-03T07:21:06.386Z"),
"date_of_cancellation" : "",
"cancelled" : "No",
"points" : 0,
"booking_id" : "ASHISH_BOOK_001",
"checked_out" : "Yes",
"booking_date" : ISODate("2015-03-03T07:21:06.386Z"
"adult_count" : 4,
"room_id" : [
"100",
"101"
],
"rooms_count" : 2,
"resort_id" : "ASH_RESORT002",
"checkout_time" : "2015-03-03T18:30:00.000Z",
"checkin_time" : "2015-03-02T18:30:00.000Z",
"guest" : "no",
"membership_id" : "1",
"__v" : 0
}
{
"_id" : ObjectId("54f6942195002c140700dac0"),
"user" : ObjectId("54ec4eb13d50decc1f5307b4"),
"created" : ISODate("2015-03-04T05:12:01.211Z"),
"date_of_cancellation" : "",
"cancelled" : "No",
"points" : 0,
"booking_id" : "ASHISH_BOOK_002",
"checked_out" : "Yes",
"booking_date" : ISODate("2015-03-04T05:12:01.211Z"
"adult_count" : 2,
"room_id" : [
"103"
],
"rooms_count" : 1,
"resort_id" : "ASH_RESORT001",
"checkout_time" : "2015-03-04T18:30:00.000Z",
"checkin_time" : "2015-03-03T18:30:00.000Z",
"guest" : "no",
"membership_id" : "1",
"__v" : 0
}
checkindetails db:
{
"_id" : ObjectId("54f06257d75533c01add0cf0"),
"user" : ObjectId("54ec4eb13d50decc1f5307b4"),
"created" : ISODate("2015-02-27T12:25:59.185Z"),
"id_proof_type" : "abcd",
"address" : "bangalore",
"guest_name" : "Harshavardhan",
"booking_id" : "ASHISH_BOOK_000",
"__v" : 0
}
{
"_id" : ObjectId("54f696b095002c140700dac4"),
"user" : ObjectId("54ec4eb13d50decc1f5307b4"),
"created" : ISODate("2015-03-04T05:22:56.498Z"),
"id_proof_type" : "okoko",
"address" : "okoko",
"guest_name" : "kok",
"booking_id" : "ASHISH_BOOK_002",
"__v" : 0
}
I am able to reach till console.log(a.booking_id);
Here I am comparing the booking_id of a(input parameter) with the booking_id of checkindetails and I want the result value of matching booking_id.
The console.log is providing me the correct answer but when I return that console.log value I am getting a blank answer.
Please Help me to find the correct One.
Thanks in advance.
According to Angularjs documentation a filter:
Selects a subset of items from array and returns it as a new array.
Think of the filter as a subset of your original data, that subset is defined within the function that is called when you use that filter.
So in your example you would define your filter "select_booking" to return the filtered values. They can be partial matches by default, or exact matches by adding.."...:true" to the end.
If you want to match the id, you should return the actual id, not "true".
return a.booking_id;
If your filter returns a single id, your ng-repeat will show all bookings with that id. As a side note, if it is an "id" I would use the exact match like...
"book in bookings|filter:{id:select_booking}:true"
That would be how to set it up. Your return type is what you are filtering. If I'm filtering an array of numbers I return numbers. If I'm filtering text, I return text ect.

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