MongoDB MapReduce. $exists on nested field - mongodb

I want to execute map-reduce operation on specific documents from MongoDB.
Let's say, we have this "document":
{
a: {
b: {
c:{}
}
}
}
I want to select only those documents, that have an a.b.c field. From this documentation page I know, that $exists operator can help.
To check, that a exists I should use:
query: {
a: { $exists : true }
}
But what query should I use for nested field, described before?
query: {
//?
}

This isn't related to the fact of using map-reduce.
query: {
'a.b.c' : { $exists : true }
}
So is a standard query on an embedded document:
http://docs.mongodb.org/manual/tutorial/query-documents/#embedded-documents

Related

mongoose search text in nested object

there is a nested mongodb document that looks like the image below. I want to do a search in the body field in this document, but I could not succeed. how can i do this search. thanks in advance
I tried to search like this. Is my path correct?
const result = await this.campaignModel.find({
"sequencesPageData": {
"$elemMatch": {
"mailSteps": {
"$elemMatch": {
"mailTemplate": x
}
}
}
}
});
I did a search like this but got no results.
const result = await this.campaignModel.find({
"sequencesPageData": {
"$elemMatch": {
"mailSteps": {
"$elemMatch": {
"mailTemplate": x
}
}
}
}
});
For object fields, simply access them using dot notation. For array fields, use $elemMatch to perform the match.
db.collection.find({
"sequencesPageData.mailSteps": {
"$elemMatch": {
"mailTemplate": {
"$elemMatch": {
"subject": "asdf"
}
}
}
}
})
Mongo Playground
If you are only testing a single field in the array, there is no need to use $elemMatch, you can use dot notation:
db.collection.find({ "sequencesPageData.mailSteps.mailTemplate.body": "x" })
$elemMatch is useful to ensure that 2 or more tests are satisfied by the same array element, but since this example uses only 1 test, it works without.

How To FindOne Doccument in Array of Object MongoDb?

I Have document like this.
{
accountId: "182092109",
documents:[{
idDocuments:"0000",
filePath :"/data/photo/1.png",
status:"EXIST"
},
{
idDocuments:"0001",
filePath :"/data/photo/2.png",
status:"EXIST"
}]
}
but i need document to be like
{
idDocuments:"0001",
filePath :"/data/photo/2.png",
status:"EXIST"
}]
}
is it possible ? or is this have alternative to findOne document in mongo into like that ?
Thank's in Advance
As far as I understand you want to find a specific document in an array of objects.
You can use the $elemMatch operation within a find() method. To return only matching results.
You can try something like this:
db.collection.find({
arrayOfObjects: {
$elemMatch: { // Finding only the ones that match
idDocuments:"0001",
filePath :"/data/photo/2.png",
status:"EXIST"
}
}
})
If your $elemMatch returns more than 1 result, you can limit the find() method using the $limit operator.
db.collection.find({
arrayOfObjects: {
$elemMatch: { // Finding only the ones that match
idDocuments:"0001",
filePath :"/data/photo/2.png",
status:"EXIST"
}
}
}, {
arrayOfObjects: {
$limit: 1 // Limiting results
}
})
Most of the information can be found in MongoDB Manual.
Or you can read more here.

mongodb query: nested elemMatch

Currently, that's my current document:
{
_id: 'd283015f-91e9-4404-9202-093c28d6a931',
referencedGeneralPractitioner: [
{
resourceType: 'practitioner',
cachedIdentifier: [
{
system: { value: 'urn:oid:1.3.6.1.4.1.19126.3' },
value: { value: '14277399B' }
}
]
}
]
}
Here, there's two nested objects arrays: referencedGeneralPractitioner[{cachedIdentifier[{}]}].
Currently, I'm getting results using this query:
{
"referencedGeneralPractitioner":{
"$elemMatch":{
"cachedIdentifier.value.value":"14277399B",
"cachedIdentifier.system.value":"urn:oid:1.3.6.1.4.1.19126.3"
}
}
}
It's getting my desired document, but I don't quite figure out if above query is which I'm really looking for.
I mean, I'm only applying $elemMatch on referencedGeneralPractitioner field array.
Is it really enought?
Should I add a nested $elemMatch on cachedIdentifier?
Any ideas?
It looks like you need to query it like this:
db.collection.find({
"referencedGeneralPractitioner.cachedIdentifier": {
"$elemMatch": {
"value.value": "14277399B",
"system.value": "urn:oid:1.3.6.1.4.1.19126.3"
}
}
})
playground
This is in case you need to find the full document having $and of both values in same element in any of the elements in the nested array , if you need to extract specific element you will need to $filter
if you need to search also based on element in the 1st array level then you need to modify as follow:
{
"referencedGeneralPractitioner": {
"$elemMatch": {
resourceType: 'practitioner',
"cachedIdentifier": {
"$elemMatch": {
"value.value": 1,
"system.value":2
}
}
}
}
}
This will give you all full documents where at same time there is resouceType:"practitioner" and { value.value:3 and system.value: 2 }
Also is important to stress that this will not gona work correctly!:
{
"referencedGeneralPractitioner":{
"$elemMatch":{
"cachedIdentifier.value.value":"14277399B",
"cachedIdentifier.system.value":"urn:oid:1.3.6.1.4.1.19126.3"
}
}
}
Since it will match false positives based on any single value in the nested elements like:
wrong playground

How to query nested mongo collection

I have a collection called 'Profiles', and it has a structure like this:
{
_id : XXXXX,
classrooms:{
owner:[],
students: [XYSk0,CCD7U],
},
...
}
I want to get profiles that has classrooms.students equals to CCD7U.
I have tried using $elemMatch on the collection publish function, but unsuccessful:
Meteor.publish('StudentsInClassrooms', function(CCD7U){
return Profiles.find({ classrooms : { $elemMatch :{ students : CCD7U }} });
});
I even tried dot notation, but still unsuccessful:
Meteor.publish('StudentsInClassrooms', function(CCD7U){
return Profiles.find({ 'classrooms.students' : CCD7U });
});
How to properly query that kind of nested collection?
You should use the "$in" operator for such query.
Profiles.find({ 'classrooms.students' : { $in :['CCD7U'] } } )
Ref: MongoDB documentation; $in
When you normally query something you would do it
Profiles.find( { 'classrooms': 'classroomName' } );
But because your are trying to find something from nested you need to do it like this
Profiles.find( { 'classrooms': { 'students' : CCD7U } } );

update nested object in MongoDB

I'm new in MongoDB.I have read almost all relative posts but cannot find a proper answer for my case. Suppose you have the document:
{ "_id" : "5ad1..",
"linkedin" : { "linkedinId" : "NB..J", "username" : "dolguldur",
"followers":[{name:"John",email:"john#example.com},{name:"Elsa",email:"elsa#example.com}]
}
}
If a follower changes his email, how can i update his record with the new one;
Use the $elemMatch operator to select your desired nested object :
db.collection.update({
followers: {
$elemMatch: {
name: _follower_name,
email: _follower_email
}
}
}, {
"followers.$.email": new_mail
});
The $ positional operator will select the first nested object that match the filter in $elemMatch and update it.