update multiple documents of specific field values based on _Id - mongodb

i have the two sample documents as :
[
{
"_id": "605b2fcb526b8b609ef97eaa",
"comments": "",
"name": "",
"user_name": ""
},
{
"_id": "605b3034asubc2bed542f88f",
"comments": "",
"name": "",
"user_name": ""
}
]
and the updated data for these two documents for key comment i have as :
[
{
"_id": "605b2fcb526b8b609ef97eaa",
"comments": "test one",
"name": "",
"user_name": ""
},
{
"_id": "605b3034asubc2bed542f88f",
"comments": "test two",
"name": "",
"user_name": ""
}
]
Here I am currently trying using a loop by ID and update the respective comment value using update
May i know how could i use update_many in this scenario ?
Any guiding links or a solution is much appreciated TIA

There is no way you can update different values to the same field in different documents in a single query to Mongo (update_many or otherwise).
You need to use a loop or you can do a Bulk.

Related

Mongodb aggregation to return data from multiple collections

I have a database with comments and users. I'm attempting to run an aggregation to get data returned in a certain format with populated nested documents (see below)
Some comments are replies to other comments, which is tracked by the reply_to property in the comment model. There can be many replies for the same comment.
What I'm attempting to do is return comment documents in an array, with all of the replies in a replies property in each comment, along with some user data (display_name).
Here is sample data:
comments
{
"_id": "63acacb169f733cf45f22e51",
"user": "63acacb169f733cf45f22e4f",
"body": "I love this recipe!",
"rating": 5,
"article_id": "Y4Iu0hIAACsAohcA",
"status": "Approved",
"createdAt": "2022-12-28T20:53:05.761Z",
"updatedAt": "2022-12-28T20:55:05.837Z",
"__v": 0
},
{
"_id": "63acb0df4f4adc6e16001fe7",
"user": "636261acb4ce6745b38d8785",
"reply_to": "63acac8569f733cf45f22e49",
"article_id": "Y4Iu0hIAACsAohcA",
"status": "Trashed",
"createdAt": "2022-12-28T21:10:55.698Z",
"updatedAt": "2022-12-28T21:13:27.544Z",
"__v": 0
},
{
"_id": "63acb16d4f4adc6e16001fef",
"user": "636261acb4ce6745b38d8785",
"body": "I Love it too!",
"reply_to": "63acac8569f733cf45f22e49",
"article_id": "Y4Iu0hIAACsAohcA",
"status": "Approved",
"createdAt": "2022-12-28T21:13:17.665Z",
"updatedAt": "2022-12-28T21:13:29.199Z",
"__v": 0
},
users
{
"_id": "63ac944ac2faa2dd59f6a492",
"display_name": "Jane Doe",
"email": "dfasfasdf#example.com",
"__v": 0
},
{
"_id": "63acacb169f733cf45f22e4f",
"display_name": "Juice Guy",
"email": "juiceguy#example.com",
"__v": 0
}
Here's an example of how I want the data returned when I query for comments by article_id, so I can display all comments and replies on the page for the user.
{
"_id": "63acacb169f733cf45f22e51",
"user": {
"display_name": "John Doe",
},
"body": "I love this recipe!",
"rating": 5,
"createdAt": "2022-12-28T20:53:05.761Z",
"updatedAt": "2022-12-28T20:55:05.837Z",
"replies": [
{
"body": "I love this too!",
"user": {
"display_name": "Juice Guy"
},
"createdAt": "2022-12-28T20:53:05.761Z",
"updatedAt": "2022-12-28T20:55:05.837Z"
}
]
}
]
I've been playing with $group and $lookup but with the nested documents I can't get all the data I want in one aggregation. Can someone help?
To get these in a single document, you would need to:
$match for documents that do not have a reply_to field (top-level comments)
$lookup from comments, matching local _id with foreign reply_to
$unwind the replies
$lookup from users matching local user with remote _id
$lookup from users matching local replies.user with remote _id
$project to select/remove fields as desred
$group by _id, retaining all of the top level fields, and $push the replies back into an array
$replaceRoot/$project to finalize the shape of the returned document

How to fetch records from mongoDB on the basis of duplicate data in multiple fields

The requirement is to fetch the document with some field having the same values in the particular collection..
For Example:
we have two documents below:
1. {
"_id": "finance100",
"status": "ACTIVE",
"customerId": "100",
"contactId": "contact_100",
"itemId": "profile_100",
"audit": {
"dateCreated": "2022-02-16T16:34:52.718539Z",
"dateModified": "2022-03-18T09:36:42.774271Z",
"createdBy": "41d38c187155427fa37c855a4d1868d1",
"modifiedBy": "41d38c187155427fa37c855a4d1868d1"
},
"location": "US"
}
2. {
"_id": "finance101",
"status": "ACTIVE",
"customerId": "100",
"contactId": "contact_100",
"itemId": "profile_100",
"audit": {
"dateCreated": "2022-02-16T16:34:52.718539Z",
"dateModified": "2022-03-18T09:36:42.774271Z",
"createdBy": "41d38c187155427fa37c855a4d1868d1",
"modifiedBy": "41d38c187155427fa37c855a4d1868d1"
},
"location": "US"
}
3. {
"_id": "finance101",
"status": "ACTIVE",
"customerId": "100",
"contactId": "contact_100",
"itemId": "profile_100",
"audit": {
"dateCreated": "2022-02-16T16:34:52.718539Z",
"dateModified": "2022-03-18T09:36:42.774271Z",
"createdBy": "41d38c187155427fa37c855a4d1868d1",
"modifiedBy": "41d38c187155427fa37c855a4d1868d1"
},
"location": "UK"
}
The following parameter should have the same values:
customerId
contactId
itemId
location
so, need the fetch those records, matching with these above parameters having the same values in all the documents.
So, it should fetch the first two records (1 and 2) because the values for customerId, contactId, itemId, and location is same in first two document and in 3rd document only the location value is different(so it will not be fetched).
Could you please share the appropriate mongo query to work for this. I tried aggeration but it did not work. Thanks in advance.
Need the fetch those records, matching with these above parameters having the same values in all the documents.

How to avoid huge json documents in mongoDB

I am new to mongoDB modelling, I have been working in a small app that just used to have one collection with all my data like this:
{
"name": "Thanos",
"age": 999,
"lastName": "whatever",
"subjects": [
{
"name": "algebra",
"mark": 999
},
{
"name": "quemistry",
"mark": 999
},
{
"name": "whatever",
"mark": 999
}
]
}
I know this is standard in mongoDB since we don't have to map relotionships to other collections like in a relational database. My problem is that my app is growing and my json, even tho it works perfectly fine, it is starting to be huge since it has a few more (and quite big) nested fields:
{
"name": "Thanos",
"age": 999,
"lastName": "whatever",
"subjects": [
{
"name": "algebra",
"mark": 999
},
{
"name": "quemistry",
"mark": 999
},
{
"name": "whatever",
"mark": 999
}
],
"tutors": [
{
"name": "John",
"phone": 2000,
"status": "father"
},
{
"name": "Anne",
"phone": 200000,
"status": "mother"
}
],
"exams": [
{
"id": "exam1",
"file": "file"
},
{
"id": "exam2",
"file": "file"
},
{
"id": "exam3",
"file": "file"
}
]
}
notice that I have simplified the json a lot, the nested fields have way more fields. I have two questions:
Is this a proper way to model Mongodb one to many relationships and how do I avoid such long json documents without splitting into more documents?
Isn't it a performance issue that I have to go through all the students just to get subjects for example?

mongoDB Compass distinct query renders no results

I've been trying to write a distinct query against my mongoDB collection housed in Atlas.
I wanted to get a list of all the distinct values captured within the "Section" attribute under its "MetaData" parent attribute. According to the mongoDB documentation the syntax to get an array of distinct values is the following syntax:
{ distinct: "<collection>", key: "<field>" }
My sample collection called "simple" holds the following documents
[{
"_id": "527c61082241f813c09c722c",
"MetaData": {
"Type": "BlogPost",
"Author": "author1",
"Section": "section1"
},
"Title": "title 1",
"Description": "..."
},
{
"_id": "527c61082241f813c09c7050",
"MetaData": {
"Type": "BlogPost",
"Author": "author1",
"Section": "section1"
},
"Title": "title 2",
"Description": "..."
},
{
"_id": "527c61082241f813c09c7042",
"MetaData": {
"Type": "BlogPost",
"Author": "author1",
"Section": "section2"
},
"Title": "title 3",
"Description": "..."
}
]
yet when I execute the following filter query in Compass or Atlas's - no results are returned ???
{distinct: 'simple',key: 'MetaData.Section'}
I was expecting an array holding "section1" and "section2"
Can someone tell what I'm doing incorrectly please
Thanks
You need to use aggregation framework for compass to get distinct values
{
$group:{
"_id": null,
"sections": { $addToSet: "$MetaData.Section"}
}
}

Using GraphQL,Springboot,MongoDB.The json is 1000+ lines deeply nested.Instead of updating whole doc,need to update specific key-value at any position

Requirement for mutation is to behave like upsert. For example in below json mutation is required to change "status" under Rooms->Availability section. Which I do not want to hard code like
db.collection.update({
'Rooms.Availability.status':true
},{
$set : {
'Rooms.Availability.status':False
}
})
for specific Array Index because there are possibilities of not having "status" or "Availability" key in some other document.
Below is similar JSON structure. Keys can be different in other JSON documents within same collection.
#GraphQLMutation(name = "updateHotelDetails")
public Hotel updateHotelDetails(Hotel h){ // Do I need to pass whole object as argument or only specific key-value?
mongoTemplate.updateFirst(....); // How can I write update code without hard coding?
}
Document 1:
{
"_id" : ObjectId("71testsrtdtsea6995432"),
"HotelName": "Test71testsrtdtsea699fff",
"Description": ".....",
"Address": {
"Street": "....",
"City": "....",
"State": "...."
},
"Rooms": [
{
"Description": "......",
"Type": ".....",
"Price": "....."
"Availability": [
"status": false,
"readOnly": false
]
},
{
"Description": "......",
"Type": "....",
"Price": "..."
"Availability": [
"status": true,
"readOnly": false
]
"newDynamickey": [
{}
]
},
]
"AdditionalData": [
{
"key1": "Vlaue1",
"key2":"Value2"
},
{...}
]
}