mongoDB Compass distinct query renders no results - mongodb

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"}
}
}

Related

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.

Need to aggregate and return results based on document nested array

I have the following document:
{
"_id": ObjectId("10..."),
"ownerName": "Merchant 10",
...,
"stores": [
{
"id": ObjectId("20..."),
"storeName": "Store 20"
},
{
"id": ObjectId("21..."),
"storeName": "Store 21"
}
]
},
{
"_id": ObjectId("11..."),
"ownerName": "Merchant 11",
...,
"stores": [
{
"id": ObjectId("22..."),
"storeName": "Store 22"
}
]
}
Is it possible with aggregate to return paginated results based on nested array "stores", and end up with the following result:
{
"_id": "20...",
"ownerName": "Merchant 10",
"storeName": "Store 20"
}, {
"_id": "21...",
"ownerName": "Merchant 10",
"storeName": "Store 21"
}, {
"_id": "22...",
"ownerName": "Merchant 11",
"storeName": "Store 22"
},
So basically, I would like to be able to share owner info but returning result based on stores.
I tried another way with a stores document but with my specific requirement, I ended up with a circular dependency.
Thanks
Using unwind on stores you can flatten out the documents. After that, just project the fields you need:
db.mycollection.aggregate( [
{ $unwind: "$stores" },
{ $project: { "ownerName": 1, "storeName": "$stores.storeName" } }
] )

update multiple documents of specific field values based on _Id

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.

Aggrerate nested objectid

I have documents like this:
{
"_id": "...",
"collectionName": "blabla",
"items": ["ObjectID("1")","ObjectID("2")"],
}
items collection:
{
"_id": "1",
"name": "item name",
"size": 3
},
{
"_id": "2"
"name": "item name 2",
"size": 4
}
Output:
{
"_id": "...",
"collectionName": "blabla",
"items": ["ObjectID("1")","ObjectID("2")"],
"totalSize": 7
}
I'm trying to use aggrerate to sum all items size which is referenced by ObjectID
is this even possible? I couldn't find any information about it

MongoDB: Return deeply nested document from it's id regardless of level

I have a collection consisting of documents that are made up of an id, name and an array of items; the array in each document is supposed to nest other documents of the same structure when applicable, and there is no limit to how many nested arrays of documents there are (nested documents can also have nested documents).
Using the MongoClient package, I'm trying to query my collection and return a document based on it's id, regardless of it's location in the data (be it at the top level or 3 levels down).
So far I can return any top level data okay, but my query is not finding any nested data. I've seen similar questions where the data structure is limited and consistent, but as my data is dynamic and multi-layered, I haven't find a solution that fits this particular issue.
Here's my data:
[
{
"_id": 1,
"name": "Test 1",
"items": []
},
{
"_id": 2,
"name": "Test 2",
"items": [
{
"_id": 3,
"name": "Test 3",
"items": []
},
{
"_id": 4,
"name": "Test 4",
"items": [
{
"_id": 6,
"name": "Test 6",
"items": []
}
]
}
]
},
{
"_id": 5,
"name": "Test 5",
"items": []
}
]
Here's my Mongo query:
MongoClient.connect(connString, function(err, db) {
var collection = db.collection('items');
collection.findOne({_id: ObjectID("4")}, function(err, result) {
console.log(result);
});
db.close();
});
And result is intended to return the following, but returns null instead:
{
"_id": 4,
"name": "Test 4",
"items": [
{
"_id": 6,
"name": "Test 6",
"items": []
}
]
}
Can anyone share a solution as to how I can retrieve my data as intended?
It is not possible to query an infinate amount of document of documents due to the change of properties at each level. See Query Selectors (https://docs.mongodb.com/manual/reference/operator/query/#query-selectors) for a list of selectors that can be used.
However if it's possbile to limit or give a depth limit on the items then you could use a query such as:-
db.col.find( {$or: [ { "_id" : id }, { "items._id" : id }, { "items.items._id" : id }, { "items.items.items._id" : id } ] } );
If it's not possible to give a depth limit I'd advise re-modeling the document in to something like:
[
{
"_id": 1,
"name": "Test 1"
},
{
"_id": 3,
"name": "Test 3",
"parentId": 2
},
{
"_id": 6,
"name": "Test 6",
"parentId": 4
},
{
"_id": 4,
"name": "Test 4",
"parentId": 2
},
{
"_id": 2,
"name": "Test 2",
},
{
"_id": 5,
"name": "Test 5",
}
]
Then you could do a simple find by _id query:
> db.collection.find({_id: 4})
{ "_id" : 4, "name" : "Test 4", "parentId" : 2 }
If you also need to retain the document structure of the query you can use the $graphLookup aggregation stage.