How to query an object in an array embedded in mongodb? - mongodb

I am currently working on an application that takes control of Projects, which have Meetings and that these meetings have Participants.
I want to consult a Participant by his nomina field.
Structure for a project document object:
{
"id":"5c1b0616a0441f27f022bfdc",
"name":"Project Test",
"area":"Area",
"date":"2019-01-01",
"meetings":[
{
"id":"5c1b073d445707834699ce97",
"objetive":"Objetive",
"fecha":"2019-01-01",
"participants":[
{
"nomina":1,
"name":"Person 1",
"role":"Rol1",
"area":"area1",
"signature":null
},
{
"nomina":2,
"name":"Person 2",
"role":"rol 2",
"area":"área 2",
"signature":null
}
]
}
]
}
Expected behavior
I want to consult a Participant by nomina field knowing the id of the Project and also knowing the id of the Meeting.
Expected output
Having:
id Project = 5c1b0616a0441f27f022bfdc
id Meeting = 5c1b073d445707834699ce97
nomina Participant = 1
It's expected that the query will return me:
{
"nomina":1,
"name":"Person 1",
"role":"Rol1",
"area":"area1",
"signature":null
}

For not so huge number of meetings in every document if you want to get the exact document stated, you can do this pipeline, it is straight forward:
db.collection.aggregate(
[
{
$match: {
id:"5c1b0616a0441f27f022bfdc"
}
}, {
$unwind: {
path : "$meetings"
}
},
{
$unwind: {
path : "$meetings.participants"
}
},
{
$match: {
"meetings.id":"5c1b073d445707834699ce97",
"meetings.participants.nomina":1
}
},
{
$replaceRoot: {
newRoot: "$meetings.participants"
}
}
]);
If you would have over thousands of elements in meetings then I'd suggest adding another match to meetings or grouping meetings and project IDs.
But if you just want to get the document containing what you want it is just a simple find query:
db.collection.find({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 });

Related

MongoDB: Update array elements on multiple conditions

Colleagues, good afternoon!
I'm struggling with some issue. I am using MongoDB 4.4.4. My assignment looks like this:
There is a set of elements methods[].subcategories[].actions[]. Please note that all objects are arrays and they may be absent. Elements of the actions[] array consists of the _id and title fields.
It is necessary to find the actual value of recordId by the field actions.title of the element and write it to the element of the actions array.
List of current values:
0af4cd2e-78cb-109b-8178-d5a7ba0e0012, Inspection
0af4cd2e-78cb-109b-8178-d5a7ba130014, Screening
0af4cd2e-78cb-109b-8178-d5a7ba170016, Poll
0af4cd2e-78cb-109b-8178-d5a7ba1b0018, Getting written explanations
0af4cd2e-78cb-109b-8178-d5a7ba1e001a, Request for documents
0af4cd2e-78cb-109b-8178-d5a7ba21001c, Sampling (samples)
0af4cd2e-78cb-109b-8178-d5a7ba23001e, Instrumental examination
0af4cd2e-78cb-109b-8178-d5a7ba260020, Test
0af4cd2e-78cb-109b-8178-d5a7ba2b0022, Expertise
0af4cd2e-78cb-109b-8178-d5a7ba2d0024, Experiment
3b7205c1-8282-4b63-8121-b82aacd7ca67, Request for documents that, in accordance with the mandatory requirements, must be located at the location (carrying out activities) of the controlled person (its branches, representative offices, separate structural divisions) or the object of control
I wrote the following code:
db.getSiblingDB("ervk_core").getCollection("supervision1").updateMany(
{},
{
"$set": {
"methods.subcategories.actions.$[elem1]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e0012",
"methods.subcategories.actions.$[elem2]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e0014",
"methods.subcategories.actions.$[elem3]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e0016",
"methods.subcategories.actions.$[elem4]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e0018",
"methods.subcategories.actions.$[elem5]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e001a",
"methods.subcategories.actions.$[elem6]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e001c",
"methods.subcategories.actions.$[elem7]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e001e",
"methods.subcategories.actions.$[elem8]._id": "0af4cd2e-78cb-109b-8178-d5a7ba260020",
"methods.subcategories.actions.$[elem9]._id": "0af4cd2e-78cb-109b-8178-d5a7ba260022",
"methods.subcategories.actions.$[elem10]._id": "0af4cd2e-78cb-109b-8178-d5a7ba260024",
"methods.subcategories.actions.$[elem11]._id": "3b7205c1-8282-4b63-8121-b82aacd7ca67",
}
},
{
"arrayFilters": [
{
"elem1.title": "Inspection",
},
{
"elem2.title": "Search",
},
{
"elem3.title": "Poll",
},
{
"elem4.title": "Receipt of Written Explanations",
},
{
"elem5.title": "Retrieval of Documents",
},
{
"elem6.title": "Sampling (samples)",
},
{
"elem7.title": "Instrumental examination",
},
{
"elem8.title": "Trial",
},
{
"elem9.title": "Expertise",
},
{
"elem10.title": "Experiment",
},
{
"elem11.title": "Request for documents that, in accordance with the mandatory requirements, must be located at the location (carrying out activities) of the controlled person (its branches, representative offices, separate structural divisions) or the object of control",
}
]
}
);
However, it gives the error "The path 'methods.subcategories.actions' must exist in the document in order to apply array updates.". I understand why it occurs - due to the absence of the actions[] array. But how can I account for the fact that methods[].subcategories[].actions[] arrays may be missing. And did I write the code correctly, otherwise I'm already a little confused. Thanks a lot in advance!
What you can do is add array filters checks to the method and subcategory object to see the nested array exists, this will solve your issue as Mongo will not continue checking the nested conditions in case they don't exist. Here's how you'd do it:
db.collection.updateMany(
{
"methods.subcategories.actions": {
$exists: true
}
},
{
"$set": {
"methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem1]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e0012",
"methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem2]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e0014",
"methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem3]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e0016",
"methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem4]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e0018",
"methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem5]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e001a",
"methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem6]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e001c",
"methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem7]._id": "0af4cd2e-78cb-109b-8178-d5a7ba0e001e",
"methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem8]._id": "0af4cd2e-78cb-109b-8178-d5a7ba260020",
"methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem9]._id": "0af4cd2e-78cb-109b-8178-d5a7ba260022",
"methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem10]._id": "0af4cd2e-78cb-109b-8178-d5a7ba260024",
"methods.$[methodElem].subcategories.$[subCatElem].actions.$[elem11]._id": "3b7205c1-8282-4b63-8121-b82aacd7ca67"
}
},
{
"arrayFilters": [
{
"methodElem.subcategories": {
$exists: true
}
},
{
"subCatElem.actions": {
$exists: true
}
},
{
"elem1.title": "Inspection"
},
{
"elem2.title": "Search"
},
{
"elem3.title": "Poll"
},
{
"elem4.title": "Receipt of Written Explanations"
},
{
"elem5.title": "Retrieval of Documents"
},
{
"elem6.title": "Sampling (samples)"
},
{
"elem7.title": "Instrumental examination"
},
{
"elem8.title": "Trial"
},
{
"elem9.title": "Expertise"
},
{
"elem10.title": "Experiment"
},
{
"elem11.title": "Request for documents that, in accordance with the mandatory requirements, must be located at the location (carrying out activities) of the controlled person (its branches, representative offices, separate structural divisions) or the object of control"
}
]
})
Mongo Playground
I also changed the updates query to just ignore documents with no nested actions, this is just to save time.

Sort records based on nested array element in MongoDB

I have modal something like this
[{
"_id":"",
"document_name":"Document 1",
"document_users" : [
{
"name":"Jhon",
"email":"jhon#gmail.com",
"added_at":"2020-12-11 10:30:01"
},
{
"name":"Michel",
"email":"michel#gmail.com",
"added_at":"2020-12-11 10:40:00"
}
]
},
{
"_id":"",
"document_name":"Document 2",
"document_users" : [
{
"name":"Jhon",
"email":"jhon#gmail.com",
"added_at":"2020-12-12 10:30:01"
},
{
"name":"Henry",
"email":"henry#gmail.com",
"added_at":"2020-12-14 10:40:00"
}
]
}]
I need to sort this main documents records based on particular user's "added_at" in the "document_users". Here email id is unique in the "document_users" field.
In nutshell, sort the main documents based on nested elements.
It will be great if I get any help how to write the mongoDB query for this one

MongoDB $addToSet inside another $addToSet

I am trying to achieve the below response from query.
{
users:[
user:{
name:"",
email:[
]
},
....user2
]
}
I have something similar to below.
I will put this way. One user can have n number of devices. He may have more than one email per device. I want to group at devices. And user field should have common information for that device. As we think, name will always be same. Device specific attributes also needed like whatKindOfDevice, howManyIssuesAreThereInThatDevice, howManyCanBeAddressedByUpgrade, howManyAreRare,etc.. along with this i need to get all the emails used in that device like owner,user,associate - all emails put.
Think my document Id is not associated with single user, single device. One user can have any number of devices. One device can have n Number of documents.
$group:{
"_id":"user_device_id",
"user": {
"$addToSet": {
"name":"$name",
"deviceName":"$deviceName",
"email": {"$addToSet":{}} //Something Similar I am expecting
}
}
}
If I add email outer user it works - but it affects the response format required.
Is it possible or any other way to get it the similar response through the query?
Let's assume one user can have more than one document. In each doc, there could be same or duplicate email IDs. I am trying to get that together.
Please advise.
Sample Doc:
{
_id:ObjectId,
name:"user1",
email:"a#yahoo.com"
},
{
_id:ObjectId,
name:"user1",
email:"a#device.com"
},
..user2Doc
..user1Doc with another category, duplicate email i.e a#yahoo.com
..user2Doc with new email
..
Well, it seems like you want to get all the email for the particular user and then group all the users.
So, to achieve that you have to do consecutive grouping stages.
I used the below documents:
[
{
name:"user1",
email:"a#yahoo.com"
},
{
name:"user1",
email:"a#device.com"
},
{
name:"user2",
email:"b#yahoo.com"
},
{
name:"user1",
email:"c#device.com"
}
]
Here is the query:
db.collection.aggregate([
{
$group:{
"_id":"$name",
"emails":{
$addToSet:"$email"
},
"name":{
$first:"$name"
}
}
},
{
$group:{
"_id":null,
"users":{
$addToSet:{
"name":"$name",
"email":"$emails"
}
}
}
},
{
$project:{
"_id":0
}
}
]).pretty()
Output:
{
"users" : [
{
"name" : "user1",
"email" : [
"c#device.com",
"a#device.com",
"a#yahoo.com"
]
},
{
"name" : "user2",
"email" : [
"b#yahoo.com"
]
}
]
}
For mare about $group refer here.
Hope this will help :)

Query Mongo Collection of nested document of same type

I have a not so unique requirement but i am just finding the right terminology to search, since i only keep getting results on how to query list/ array fields or nested elements.
Here is my class (document type)...
public class Item {
private String identity = null;
private String name = null;
private String type = null;
private List<Item> grouping = null;
}
Thus, some entity instances of this type could get complex as shown below...
{
"identity":"ITEM-1",
"name":"Pack-1",
"type":"Bundle",
"grouping":[
{
"identity":"ITEM-2",
"name":"Book",
"type":"Atomic Unit"
},
{
"identity":"ITEM-3",
"name":"Stationary",
"type":"Bundle",
"grouping":[
{
"identity":"ITEM-4",
"name":"Pen",
"type":"Atomic Unit"
},
{
"identity":"ITEM-5",
"name":"Paper",
"type":"Atomic Unit"
},
{
"identity":"ITEM-6",
"name":"Paraphernalia",
"type":"Bundle",
"grouping":[
{
"identity":"ITEM-7",
"name":"Eraser",
"type":"Atomic Unit"
},
{
"identity":"ITEM-8",
"name":"Ruler",
"type":"Atomic Unit"
},
{
"identity":"ITEM-9",
"name":"Compass",
"type":"Atomic Unit"
}
]
}
]
}
]
}
Now my requirement is to be able to search for Book or Pen or Compass and must be able to fetch the record ITEM-1. How do I achieve this in Mongo Query. I am using Spring Data Mongo Repository approach on my data abstraction service layer.
Thanks in advance for all the help.
my requirement is to be able to search for Book or Pen or Compass and
must be able to fetch the record ITEM-1.
You can try this query from mongo shell, and it fetches the document:
var SEARCH_ITEMS = [ "Book", "Pen", "Compass" ]
var ATOMIC = "Atomic Unit"
db.collection.find( {
$or: [
{ "type": ATOMIC, name: { $in: SEARCH_ITEMS } },
{ "grouping.type": ATOMIC, "grouping.name": { $in: SEARCH_ITEMS } },
{ "grouping.grouping.type": ATOMIC, "grouping.grouping.name": { $in: SEARCH_ITEMS } },
{ "grouping.grouping.grouping.type": ATOMIC, "grouping.grouping.grouping.name": { $in: SEARCH_ITEMS } }
]
} )

I need to extract data in the form of key-value pairs from a collection of records and merge them into their parent record in mongoDB

Below I have a structure for supporting custom picklist fields (in this example) within my sails.js application. The general idea is we support a collection of custom picklist values on any model within the app and the customer can have total control of the configuration of the custom field.
I went with this relationship model as using a simple json field lacks robustness when it comes to updating each individual custom picklist value. If I allow a customer to change "Internal" to "External" I need to update all records that have the value "Internal" recorded against that custom picklist with the new value.
This way - when I update the "value" field of CustomPicklistValue wherever that record is referenced via ID it will use the new value.
Now the problem comes when I need to integrate this model into my existing report engine...
rawCollection
.aggregate(
[
{
$match: {
createdAt: {
$gte: rangeEndDate,
$lte: rangeStartDate
},
...$match
}
},
{
$project: {
...$project,
total: $projectAggregation
}
},
{
$group: {
_id: {
...$groupKey
},
total: {
[`$${aggrAttrFunc}`]: "$total"
}
}
}
],
{
cursor: {
batchSize: 100
}
}
)
Here is the main part of a method for retrieving and aggregating any models stored in my mongodb instance. A user can specify a whole range of things including but not limited to the model, field specific date ranges and filters such as "where certificate status equals expired" etc.
So I'm now presented with this data structure:
{
id: '5e5fb732a9422a001146509f',
customPicklistValues: [
{
id: '5e4e904f16ab94bff1a324a0',
value: 'Internal',
fieldName: 'Business Group',
customPicklist: '109c7a1a9d00b664f2ee7827'
},
{
id: '5e4e904f16ab94bff1a324a4',
value: 'Slack',
fieldName: 'Application',
customPicklist: '109c5a1a9d00b664f2ee7827'
}
],
}
And for the life of me can't work out if there's any way I can essentially pull out fieldName and value for each of the populated records as key-value pairs and add each to the parent record before running my match clause...
I think I need to use lookup to initially populate the customPicklistValues and then merge them somehow?
Any help appreciated.
EDIT:
#whoami has suggested I use $addFields. There was a fair amount I needed to do before $addFields to populate the linked records (due to how Waterline via sails.js handles saving Mongo ObjectIDs in related collections as strings), you can see my steps in compass here:
Final step would be to edit this or add a stage to it to actually be able to support a key:value pair like Business Group: "Finance" in this example.
You can try these stages after your $lookup stage :
db.collection.aggregate([
{
$addFields: {
customPicklistValues:
{
$arrayToObject: {
$map: {
input: '$customPicklistValues',
in: { k: '$$this.fieldName', v: '$$this.value' }
}
}
}
}
},
{ $replaceRoot: { newRoot: { $mergeObjects: ['$customPicklistValues', '$$ROOT'] } } },
{ $project: { customPicklistValues: 0 } }
])
Test : MongoDB-Playground