how filter inside array in mongodb - mongodb

I want to filter only kumar "to" items"_id" : ObjectId("5048d2e5fbdac48208000042") message from my query and expected result
{
"_id" : ObjectId("5191502a2f1b3ca33e000016"),
"message" : "<p>sssdasd<br></p>",
"subject" : "test message to nag",
"date" : ISODate("2013-05-13T20:42:19.349Z")
}
from below collections
{
"__v" : 0,
"_id" : ObjectId("5191502a2f1b3ca33e000016"),
"conversation" : [
{
"date" : ISODate("2013-05-13T20:42:19.349Z"),
"message" : "<p>sssdasd<br></p>",
"_id" : ObjectId("5191502a2f1b3ca33e000017"),
"to" : {
"_id" : ObjectId("5048d2e5fbdac48208000042"),
"name" : "Kumar"
},
"from" : {
"_id" : ObjectId("503fdbfa294f6db74de649ea"),
"name" : "Anand"
}
},
{
"message" : "<p>reply</p>",
"date" : ISODate("2013-05-13T21:05:33.453Z"),
"_id" : ObjectId("5191559c7d2c386741000018"),
"to" : {
"_id" : ObjectId("503fdbfa294f6db74de649ea"),
"name" : "Anand"
},
"from" : {
"_id" : ObjectId("5048d2e5fbdac48208000042"),
"name" : "Kumar"
}
},
{
"message" : "<p>reply2<br></p>",
"date" : ISODate("2013-05-13T21:05:55.006Z"),
"_id" : ObjectId("519155b1ca98b66641000014"),
"to" : {
"_id" : ObjectId("503fdbfa294f6db74de649ea"),
"name" : "Anand"
},
"from" : {
"_id" : ObjectId("503fdbfa294f6db74de649ea"),
"name" : "Anand"
}
}
],
"from" : {
"_id" : ObjectId("503fdbfa294f6db74de649ea"),
"name" : "Anand"
},
"sent" : ISODate("2013-05-13T20:42:19.349Z"),
"subject" : "test message to nag",
"to" : {
"_id" : ObjectId("5048d2e5fbdac48208000042"),
"name" : "Kumar"
}
}
I was try with below query
db.messages.find({'conversation.to._id':ObjectId("5048d2e5fbdac48208000042")},{'subject':-1, 'conversation.message': 1,'conversation.to':1}).pretty();
But I am not get the expected result

I have used aggregate framework I am able to get the expected result.
db.message.aggregate(
{ $unwind : "$conversation" },
{ $match: {
"conversation.to.name" : "Anand"
}
},
{
$sort: {
"conversation.date" : -1
}
},
{
$group: {
_id:"$_id",
msgSubject: {$first:"$subject"},
msgDate: {$first:"$conversation.date"},
latestmsg: {$first:"$conversation.message"}
}
}
);
Please share if any different than above

Related

$lookup aggregation failed to display array value

I'm working with MongoDB and I've 2 collections.
data collection ["user-profile"]
{
"_id" : ObjectId("60650e6fc4b4603e1e78bb23"),
"firstName" : "Luthfan",
"lastName" : "Difiesa",
"mobile" : "86742633497",
"gender" : "male",
"createdOn" : ISODate("2021-04-22T05:26:07.428+0000"),
"updatedOn" : ISODate("2021-04-22T05:26:55.218+0000")
}
data collection ["user-wishlist"]
{
"_id" : ObjectId("60650e7a1a4a817a1dd0a29c"),
"userId" : "86742633497",
"contents" : [
{
"_id" : ObjectId("5ef9d2da228f840bbd41649c"),
"name" : "Kelas 11"
}
]
}
expected output:
{
"_id" : ObjectId("60650e6fc4b4603e1e78bb23"),
"firstName" : "Luthfan",
"lastName" : "Difiesa",
"mobile" : "86742633497",
"gender" : "male",
"contents" : [
{
"_id" : ObjectId("5ef9d2da228f840bbd41649c"),
"name" : "Kelas 11"
}
]
}
Here's the query:
db.getCollection("user-profile").aggregate(
[
{
"$lookup" : {
"from" : "user-wishlist",
"localField" : "mobile",
"foreignField" : "userId",
"as" : "contents"
}
}
],
{
"allowDiskUse" : false
}
);
But the result is like this:
{
"_id" : ObjectId("60650e6fc4b4603e1e78bb23"),
"firstName" : "Luthfan",
"lastName" : "Difiesa",
"mobile" : "86742633497",
"gender" : "male",
"contents" : [
]
}
is't because of collection name using special character or type data from foreign or localField? thank u...
Your query looks good, just need add a stage after lookup to achieve your desire result,
{
$addFields: {
contents: {
$arrayElemAt: ["$contents.contents", 0]
}
}
}
Playground

Update query on in the collection by "_id"

{
"_id" : "tenant/data/EMAIL/ENGLISH",
"tenantId" : "tenant2",
"channelType" : "EMAIL",
"template" : [
{
"_id" : "1",
"templateName" : "abc",
"effectiveStartDate" : ISODate("2017-01-01T12:00:00.000Z"),
"modifiedDate" : ISODate("2017-06-02T22:08:55.782Z"),
"active" : false
}
]
}
I need to update the "templateName" : "xyz" on the basis of "_id" : "tenant/data/EMAIL/ENGLISH"
I have tried these queries but got no success
db.getCollection('data').updateOne({"_id": "tenant/data/EMAIL/ENGLISH"},
{$set : { "template.$.templateName" : "XYZ"}}); 
db.getCollection('data').updateOne({"_id": "tenant/data/EMAIL/ENGLISH"},
{$set : { "template.templateName" : "XYZ"}}); 
Any help will be appreciated.
I have used positional-all operator to update the array.
Here is the query:
db.sample.update(
{
"_id": "tenant/data/EMAIL/ENGLISH"
},
{
$set:{
"template.$[].templateName":"XYZ"
}
}
)
Output
{
"_id" : "tenant/data/EMAIL/ENGLISH",
"tenantId" : "tenant2",
"channelType" : "EMAIL",
"template" : [
{
"_id" : "1",
"templateName" : "XYZ",
"effectiveStartDate" : ISODate("2017-01-01T12:00:00Z"),
"modifiedDate" : ISODate("2017-06-02T22:08:55.782Z"),
"active" : false
}
]
}
hope this will help :)

How to find Immediate Child of a node in mongo db

I have following Collection
Location Collection
[
{id : 1 name : 'l1' , 'location' : pune , parentLocation : Maharashstra},
{id : 2 name : 'l2' , 'location' : nashik , parentLocation : Maharashstra},
{id : 3 name : 'l3' , 'location' : mumbai , parentLocation : Maharashstra},
{id : 4 name : 'l4' , 'location' : Maharashstra , parentLocation : India},
{id : 5 name : 'l5' , 'location' : India , parentLocation : null}
]
Is any query we throw and get immediate node of location using above data.
Example.
When I said India it should be return
India
|---Maharashtra
|---Pune
|---..
|---Nashik
|---Mumbai
Thank you
Using Aggregation framework we can get this desired result.
The below query gives us the result and in this query I have used $lookup, $match, $project
db.location.aggregate([
{
$lookup: {
from: "location",
localField: "location",
foreignField: "parentLocation",
as:"Result"
}
},
{$match:{ "Result": {$exists:true, $ne:[]}, parentLocation: {$ne: null} }},
{$project :{ parentLocation:1, location:1, "Result.location":1}},
{$match: {location: "Maharashstra"}}
])
Documents in my collection
{ "_id" : ObjectId("5b83e4860c35ef57411a575b"), "id" : 1, "name" : "l1", "location" : "pune", "parentLocation" : "Maharashstra" }
{ "_id" : ObjectId("5b83e4860c35ef57411a575c"), "id" : 2, "name" : "l2", "location" : "nashik", "parentLocation" : "Maharashstra" }
{ "_id" : ObjectId("5b83e4860c35ef57411a575d"), "id" : 3, "name" : "l3", "location" : "mumbai", "parentLocation" : "Maharashstra" }
{ "_id" : ObjectId("5b83e4860c35ef57411a575e"), "id" : 4, "name" : "l4", "location" : "Maharashstra", "parentLocation" : "India" }
{ "_id" : ObjectId("5b83e4860c35ef57411a575f"), "id" : 5, "name" : "l5", "location" : "India", "parentLocation" : null }
{ "_id" : ObjectId("5b83fec90c35ef57411a5760"), "id" : 6, "name" : "l6", "location" : "Chennai", "parentLocation" : "Tamilnadu" }
{ "_id" : ObjectId("5b83fec90c35ef57411a5761"), "id" : 7, "name" : "l7", "location" : "Trichy", "parentLocation" : "Tamilnadu" }
{ "_id" : ObjectId("5b83fec90c35ef57411a5762"), "id" : 8, "name" : "l8", "location" : "Alapuzha", "parentLocation" : "Kerala" }
{ "_id" : ObjectId("5b83fec90c35ef57411a5763"), "id" : 9, "name" : "l9", "location" : "Kerala", "parentLocation" : "India" }
{ "_id" : ObjectId("5b83fec90c35ef57411a5764"), "id" : 10, "name" : "l10", "location" : "Tamilnadu", "parentLocation" : "India" }
After executing the above query, the result is
{
"_id" : ObjectId("5b83e4860c35ef57411a575e"),
"location" : "Maharashstra",
"parentLocation" : "India",
"Result" : [
{
"location" : "pune"
},
{
"location" : "nashik"
},
{
"location" : "mumbai"
}
]
}
If we cut down the last $match in our query, then it will return the complete the grouping of states.
db.location.aggregate([
{
$lookup: {
from: "location",
localField: "location",
foreignField: "parentLocation",
as:"Result"
}
},
{$match:{ "Result": {$exists:true, $ne:[]}, parentLocation: {$ne: null} }},
{$project :{ parentLocation:1, location:1, "Result.location":1}}
])
The result of the above query is
{
"_id" : ObjectId("5b83e4860c35ef57411a575e"),
"location" : "Maharashstra",
"parentLocation" : "India",
"Result" : [
{
"location" : "pune"
},
{
"location" : "nashik"
},
{
"location" : "mumbai"
}
]
}
{
"_id" : ObjectId("5b83fec90c35ef57411a5763"),
"location" : "Kerala",
"parentLocation" : "India",
"Result" : [
{
"location" : "Alapuzha"
}
]
}
{
"_id" : ObjectId("5b83fec90c35ef57411a5764"),
"location" : "Tamilnadu",
"parentLocation" : "India",
"Result" : [
{
"location" : "Chennai"
},
{
"location" : "Trichy"
}
]
}

MongoDB $lookup on array of object

I have 2 collections structured as below. I have tried $lookup to get the result but I am not getting any result because of my local and foreign fields are in array of object.
Below is my structure:
{
"_id" : ObjectId("5795a3531d3f3afc19caefef"),
"name" : "category1",
"updatedAt" : "1469431592786",
"resources" : [
{
"_id" : ObjectId("5791be003fa3bedc15d3adde"),
"title" : "resource1",
"availability" : false
},
{
"_id" : ObjectId("5795a3771d3f3afc19caeff0"),
"title" : "resource2",
"availability" : true
}
]
}
Above "categories" schema have resources array of object. this resource _id is stored in bookings collection in following way:
"booking":
{
"_id" : ObjectId("57960aa8000ca7a46b7ef683"),
"name" : "1469491200000",
"__v" : 0,
"schedule" : [
{
"resourceId" : ObjectId("5791be003fa3bedc15d3adde"),
"userId" : ObjectId("5791be003fa3bedc15d3adcve"),
"title" : "grofep",
"_id" : ObjectId("57960aa8f9f9951c1fc923b1")
},
{
"resourceId" : ObjectId("5791be003fa3bedc15d3bddz"),
"userId" : ObjectId("5791be003fa3bedc15d3adcve"),
"title" : "mr3",
"_id" : ObjectId("57960aa8f9f9951c1fc923b2")
},
{
"resourceId" : ObjectId("5791be003fa3bedc15d3adde"),
"userId" : ObjectId("5791be003fa3bedc15d3adcve"),
"title" : "grofep23",
"_id" : ObjectId("57960aa8f9f9951c1fc923b3")
}
]
}
Now I want to get all the schedule of booking collection with their resource information.I want to fetch resources from categories table on the basis of booking schedule.
Desired output:
[
{
"name" : "1469491200000",
"resourceId" : ObjectId("5791be003fa3bedc15d3adde"),
"resourceTitle":"title",
"availability":false,
"bookings": [
{
"userId" : ObjectId("5791be003fa3bedc15d3adcve"),
"title" : "grofep",
"_id" : ObjectId("57960aa8f9f9951c1fc923b1")
},
{
"userId" : ObjectId("5791be003fa3bedc15d3adcve"),
"title" : "grofep23",
"_id" : ObjectId("57960aa8f9f9951c1fc923b3")
}
]
},
{
"name" : "1469491200000",
"resourceId" : ObjectId("5791be003fa3bedc15d3bddz"),
"resourceTitle":"mr3",
"availability":false,
"bookings": [
{
"userId" : ObjectId("5791be003fa3bedc15d3adcve"),
"title" : "mr3",
"_id" : ObjectId("57960aa8f9f9951c1fc923b2")
}
]
}
]
Help me to get this desired result.
Thanks.

Compound GeoSpatial Index in MongoDB is not working as intended

the collection nodesWays has the following indexes:
> db.nodesWays.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "h595.nodesWays"
},
{
"v" : 1,
"key" : {
"amenity" : 1,
"geo" : "2dsphere"
},
"name" : "amenity_1_geo_2dsphere",
"ns" : "h595.nodesWays",
"2dsphereIndexVersion" : 2
},
{
"v" : 1,
"key" : {
"geo" : "2dsphere"
},
"name" : "geo_2dsphere",
"ns" : "h595.nodesWays",
"2dsphereIndexVersion" : 2
}
]
Now the following two queries should return the same result, but they don't.
I want the nearest 10 restaurants to the specified point.
The first query is working how it should be, the second is not working like intended.
The only difference between these two queries is that the first one uses the geo_2dsphere-Index
and the second query the amenity_1_geo_2dsphere-Index.
> db.nodesWays.find(
{
geo:{
$nearSphere:{
$geometry:{
type: "Point", coordinates: [9.7399777,52.3715156]
}
}
}, "amenity":"restaurant",
name: {$exists: true}
}, {id:1, name:1}).hint( "geo_2dsphere" ).limit(10)
{ "_id" : ObjectId("53884860e552e471be2b7192"), "id" : "321256694", "name" : "Masa" }
{ "_id" : ObjectId("53884860e552e471be2b7495"), "id" : "323101271", "name" : "Bavarium" }
{ "_id" : ObjectId("53884862e552e471be2ba605"), "id" : "442496282", "name" : "Naxos" }
{ "_id" : ObjectId("53884860e552e471be2b7488"), "id" : "323101189", "name" : "Block House" }
{ "_id" : ObjectId("53884878e552e471be2d1a41"), "id" : "2453236451", "name" : "Maestro" }
{ "_id" : ObjectId("53884870e552e471be2c8aab"), "id" : "1992166428", "name" : "Weinstube Leonardo Ristorante" }
{ "_id" : ObjectId("53884869e552e471be2c168b"), "id" : "1440320284", "name" : "Altdeutsche küche" }
{ "_id" : ObjectId("53884861e552e471be2b88f7"), "id" : "353119010", "name" : "Mövenpick" }
{ "_id" : ObjectId("5388485de552e471be2b2c86"), "id" : "265546900", "name" : "Miles" }
{ "_id" : ObjectId("53884863e552e471be2bb5d3"), "id" : "532304135", "name" : "Globetrotter" }
> db.nodesWays.find(
{
geo:{
$nearSphere:{
$geometry:{
type: "Point", coordinates: [9.7399777,52.3715156]
}
}
}, "amenity":"restaurant",
name: {$exists: true}
}, {id:1, name:1}).hint( "amenity_1_geo_2dsphere" ).limit(10)
{ "_id" : ObjectId("53884875e552e471be2cf4a8"), "id" : "2110027373", "name" : "Schloßhof Salder" }
{ "_id" : ObjectId("5388485be552e471be2aff19"), "id" : "129985174", "name" : "Balkan Paradies" }
{ "_id" : ObjectId("5388485be552e471be2afeb4"), "id" : "129951134", "name" : "Asia Dragon" }
{ "_id" : ObjectId("53884863e552e471be2ba811"), "id" : "450130115", "name" : "Kings Palast" }
{ "_id" : ObjectId("53884863e552e471be2ba823"), "id" : "450130135", "name" : "Restaurant Montenegro" }
{ "_id" : ObjectId("53884877e552e471be2d053a"), "id" : "2298722569", "name" : "Pizzaria Da-Lucia" }
{ "_id" : ObjectId("53884869e552e471be2c152e"), "id" : "1420101752", "name" : "Napoli" }
{ "_id" : ObjectId("5388485be552e471be2b0028"), "id" : "136710095", "name" : "Europa" }
{ "_id" : ObjectId("53884862e552e471be2ba5bc"), "id" : "442136241", "name" : "Syrtaki" }
{ "_id" : ObjectId("53884863e552e471be2ba763"), "id" : "447972565", "name" : "Pamukkale" }
My goal with the second index is to:
select all restaurants
then use the nearSphere-Operator to sort them in regards to the distance from the specified point
Auf Wiedersehen
I think you should try to put the geolocation first in the index.