Add user id in my profile collection using aggregation - mongodb

Below is my user collection data
user collection
{
"_id" : ObjectId("584bc9ba420a6b189c510af6"),
"old_user_id" :1,
"name" :"aaa"
},
{
"_id" : ObjectId("9ba420a584bc6b189c59ba42"),
"old_user_id" : 2,
"name" :"bbb"
},
{
"_id" : ObjectId("59ba4284bc0a6b189c3323w23"),
"old_user_id" : 3,
"name" :"ccc"
}
myprofile collection
{
"old_user_id" :1,
"name" :"aaa",
"number":"123456789"
},
{
"old_user_id" : 2,
"name" :"bbb",
"number":"678912345"
},
{
"old_user_id" : 3,
"name" :"ccc",
"number":"673458912"
},
{
"old_user_id" : 2,
"name" : "bbb",
"adress" : "afsfdidhddk"
}
My expectation:
I need to match old_user_id in both collections and update the user collection '_id' in my profile collection
{
"userid":"584bc9ba420a6b189c510af6",
"old_user_id" :1,
"name" :"aaa",
"number":"123456789"
},
{
"userid":"9ba420a584bc6b189c59ba42",
"old_user_id" : 2,
"name" :"bbb",
"number":"678912345"
},
{
"userid":"59ba4284bc0a6b189c3323w23",
"old_user_id" : 3,
"name" :"ccc",
"number":"673458912"
},
{
"old_user_id" : 2,
"name" : "bbb",
"adress" : "afsfdidhddk"
"userid" : "9ba420a584bc6b189c59ba42"
}

You can use mongo aggregation. Use of $lookup, $project and $unwind will help.
I have formulated the query, hope this helps:
db.myprofile.aggregate([
{
$lookup:
{
from: "user",
localField: "old_user_id",
foreignField: "old_user_id",
as: "inventory_docs"
}
},
{ $project : { _id : "$inventory_docs._id",
old_user_id:"$old_user_id",
"name":"$name",
"number":"$number" } },
{$unwind: "$_id"}
])

Update:
db.user.find().forEach(function (user) {
var cursor = db.myprofile.find({"old_user_id": user.old_user_id});
cursor.forEach(function(myprofile) {
myprofile.userid = user._id.str;
db.myprofile.save(myprofile);
});
});
Result:
db.myprofile.find().pretty()
{
"_id" : ObjectId("584e7294678ae15db4fab039"),
"old_user_id" : 1,
"name" : "aaa",
"number" : "123456789",
"userid" : "584e7294678ae15db4fab035"
}
{
"_id" : ObjectId("584e7294678ae15db4fab03a"),
"old_user_id" : 2,
"name" : "bbb",
"number" : "678912345",
"userid" : "584e7294678ae15db4fab036"
}
{
"_id" : ObjectId("584e7294678ae15db4fab03b"),
"old_user_id" : 3,
"name" : "ccc",
"number" : "673458912",
"userid" : "584e7294678ae15db4fab037"
}
{
"_id" : ObjectId("584e7294678ae15db4fab03c"),
"old_user_id" : 2,
"name" : "bbb",
"adress" : "afsfdidhddk",
"userid" : "584e7294678ae15db4fab036"
}
Note: the _id fiels still appears in the resulting documents, but you should be able to live with that. Is a default behaviour of MongoDB. You can create a collection with no indexed _id field like in here: db.createCollection("user", { autoIndexId: false })

Related

Lookup stage not working after unwind operation in aggregate pipeline in mongodb

Being relatively new in mongodb I performed an aggregation pipeline with this three table and surprisingly I am getting the "r2" array as empty in the third lookup stage of the pipeline.I crosschecked the field names and everything is alright.
db.user.aggregate([
{
$lookup:{
from:"enrollment",
localField:"user_id",
foreignField:"userID",
as:"r1"
}
},
{
$unwind:{
path:"$r1",
includeArrayIndex:"r1_id"
}
},
{
$lookup:{
from:"course",
localField:"r1.courseID",
foreignField:"courseID",
as:"r2"
}
}
])
I have three collection as user , enrollment and course which are.
User as
> db.user.find()
{ "_id" : ObjectId("5ef4ba8d500ac8876da0d2ca"), "user_id" : 1, "first_name" : "Christian",
"last_name" : "Hur", "email" : "christian#uta.com", "password" : "abc1234" }
{ "_id" : ObjectId("5ef4ba8d500ac8876da0d2cb"), "user_id" : 2, "first_name" : "Mary", "last_name" :
"Jane", "email" : "mary.jane#uta.com", "password" : "password123" }
{ "_id" : ObjectId("5ef4bc2563742adee5403b1d"), "user_id" : 3, "first_name" : "ari", "last_name" :
"dutta", "email" : "dutta#uta.com", "password" : "po1234" }
And , course as
> db.course.find()
{ "_id" : ObjectId("5ef4c1b64a77aec0af5e73ae"), "courseID" : 3333, "title" : "Adv PHP 201",
"description" : "Advance PHP programming", "credits" : 3, "term" : "fall" }
{ "_id" : ObjectId("5ef4c20d4a77aec0af5e73af"), "courseID" : 5555, "title" : "Java 201",
"description" : "Advanced Programming", "credits" : 4, "term" : "fall"}
{ "_id" : ObjectId("5ef4c2564a77aec0af5e73b0"), "courseID" : 6666, "title" : "Angular 1",
"description" : "Intro to Angular", "credits" : 3, "term" : "fall,spring" }
And enrollment as
> db.enrollment.find()
{ "_id" : ObjectId("5ef771f42d98ffab4460a651"), "userID" : 1, "courseID" : "3333" }
{ "_id" : ObjectId("5ef7722d2d98ffab4460a652"), "userID" : 1, "courseID" : "6666" }
Result
{ "_id" : ObjectId("5ef4ba8d500ac8876da0d2ca"), "user_id" : 1, "first_name" :
"Christian", "last_name" : "Hur", "email" : "christian#uta.com", "password" :
"abc1234", "r1" : { "_id" : ObjectId("5ef771f42d98ffab4460a651"), "userID" :
1, "courseID" : "3333" }, "r1_id" : NumberLong(0), "r2" : [ ] }
{ "_id" : ObjectId("5ef4ba8d500ac8876da0d2ca"), "user_id" : 1, "first_name" :
"Christian", "last_name" : "Hur", "email" : "christian#uta.com", "password" :
"abc1234", "r1" : { "_id" : ObjectId("5ef7722d2d98ffab4460a652"), "userID" :
1, "courseID" : "6666" }, "r1_id" : NumberLong(1), "r2" : [ ] }
I also checked the documentation but found no help , how can I fix this ?
You have a type mismach on courseID between the collections, in enrollment it's type string and in course it's type number.
Change your $lookup into this:
{
$lookup: {
from: "course",
let: {
courseID: {
$toInt: "$r1.courseID"
}
},
pipeline: [
{
$match: {
$expr: {
$eq: [
"$$courseID",
"$courseID"
]
}
}
}
],
as: "r2"
}
}
Try it yourself:
Mongo Playground
Also since you say you're new I personally advice you to use the _id field instead of the courseID/user_id that you generated yourself. it will just make it easier to maintain.
For Mongo version 4.2+ here is how to update the enrollment collection field:
db.enrollment.updateMany(
{},
[
{
$set: {
courseID: {$toInt: "$courseID"}
}
}
]
)

What is $$ROOT in MongoDB aggregate and how it works?

I am watching a tutorial I can understand how this aggregate works, What is the use of pings, $$ROOT in it.
client = pymongo.MongoClient(MY_URL)
pings = client['mflix']['watching_pings']
cursor = pings.aggregate([
{
"$sample": { "size": 50000 }
},
{
"$addFields": {
"dayOfWeek": { "$dayOfWeek": "$ts" },
"hourOfDay": { "$hour": "$ts" }
}
},
{
"$group": { "_id": "$dayOfWeek", "pings": { "$push": "$$ROOT" } }
},
{
"$sort": { "_id": 1 }
}
]);
Let's assume that our collection looks like below:
{
"_id" : ObjectId("b9"),
"key" : 1,
"value" : 20,
"history" : ISODate("2020-05-16T00:00:00Z")
},
{
"_id" : ObjectId("ba"),
"key" : 1,
"value" : 10,
"history" : ISODate("2020-05-13T00:00:00Z")
},
{
"_id" : ObjectId("bb"),
"key" : 3,
"value" : 50,
"history" : ISODate("2020-05-12T00:00:00Z")
},
{
"_id" : ObjectId("bc"),
"key" : 2,
"value" : 0,
"history" : ISODate("2020-05-13T00:00:00Z")
},
{
"_id" : ObjectId("bd"),
"key" : 2,
"value" : 10,
"history" : ISODate("2020-05-16T00:00:00Z")
}
Now based on the history field you want to group and insert the whole documents in to an array field 'items'. Here $$ROOT variable will be helpful.
So, the aggregation query to achieve the above will be:
db.collection.aggregate([{
$group: {
_id: '$history',
items: {$push: '$$ROOT'}
}
}])
It will result in following output:
{
"_id" : ISODate("2020-05-12T00:00:00Z"),
"items" : [
{
"_id" : ObjectId("bb"),
"key" : 3,
"value" : 50,
"history" : ISODate("2020-05-12T00:00:00Z")
}
]
},
{
"_id" : ISODate("2020-05-13T00:00:00Z"),
"items" : [
{
"_id" : ObjectId("ba"),
"key" : 1,
"value" : 10,
"history" : ISODate("2020-05-13T00:00:00Z")
},
{
"_id" : ObjectId("bc"),
"key" : 2,
"value" : 0,
"history" : ISODate("2020-05-13T00:00:00Z")
}
]
},
{
"_id" : ISODate("2020-05-16T00:00:00Z"),
"items" : [
{
"_id" : ObjectId("b9"),
"key" : 1,
"value" : 20,
"history" : ISODate("2020-05-16T00:00:00Z")
},
{
"_id" : ObjectId("bd"),
"key" : 2,
"value" : 10,
"history" : ISODate("2020-05-16T00:00:00Z")
}
]
}
I hope it helps.

MongoDB: Sort in combination with Aggregation group

I have a collection called transaction with below documents,
/* 0 */
{
"_id" : ObjectId("5603fad216e90d53d6795131"),
"statusId" : "65c719e6727d",
"relatedWith" : "65c719e67267",
"status" : "A",
"userId" : "100",
"createdTs" : ISODate("2015-09-24T13:15:36.609Z")
}
/* 1 */
{
"_id" : ObjectId("5603fad216e90d53d6795134"),
"statusId" : "65c719e6727d",
"relatedWith" : "65c719e6726d",
"status" : "B",
"userId" : "100",
"createdTs" : ISODate("2015-09-24T13:14:31.609Z")
}
/* 2 */
{
"_id" : ObjectId("5603fad216e90d53d679512e"),
"statusId" : "65c719e6727d",
"relatedWith" : "65c719e6726d",
"status" : "C",
"userId" : "100",
"createdTs" : ISODate("2015-09-24T13:13:36.609Z")
}
/* 3 */
{
"_id" : ObjectId("5603fad216e90d53d6795132"),
"statusId" : "65c719e6727d",
"relatedWith" : "65c719e6726d",
"status" : "D",
"userId" : "100",
"createdTs" : ISODate("2015-09-24T13:16:36.609Z")
}
When I run the below Aggregation query without $group,
db.transaction.aggregate([
{
"$match": {
"userId": "100",
"statusId": "65c719e6727d"
}
},
{
"$sort": {
"createdTs": -1
}
}
])
I get the result in expected sorting order. i.e Sort createdTs in descending order (Minimal result)
/* 0 */
{
"result" : [
{
"_id" : ObjectId("5603fad216e90d53d6795132"),
"createdTs" : ISODate("2015-09-24T13:16:36.609Z")
},
{
"_id" : ObjectId("5603fad216e90d53d6795131"),
"createdTs" : ISODate("2015-09-24T13:15:36.609Z")
},
{
"_id" : ObjectId("5603fad216e90d53d6795134"),
"createdTs" : ISODate("2015-09-24T13:14:31.609Z")
},
{
"_id" : ObjectId("5603fad216e90d53d679512e"),
"createdTs" : ISODate("2015-09-24T13:13:36.609Z")
}
],
"ok" : 1
}
If I apply the below aggregation with $group, the resultant is inversely sorted(i.e Ascending sort)
db.transaction.aggregate([
{
"$match": {
"userId": "100",
"statusId": "65c719e6727d"
}
},
{
"$sort": {
"createdTs": -1
}
},
{
$group: {
"_id": {
"statusId": "$statusId",
"relatedWith": "$relatedWith",
"status": "$status"
},
"status": {$first: "$status"},
"statusId": {$first: "$statusId"},
"relatedWith": {$first: "$relatedWith"},
"createdTs": {$first: "$createdTs"}
}
}
]);
I get the result in inverse Order i.e. ** Sort createdTs in Ascending order**
/* 0 */
{
"result" : [
{
"_id" : ObjectId("5603fad216e90d53d679512e"),
"createdTs" : ISODate("2015-09-24T13:13:36.609Z")
},
{
"_id" : ObjectId("5603fad216e90d53d6795134"),
"createdTs" : ISODate("2015-09-24T13:14:31.609Z")
},
{
"_id" : ObjectId("5603fad216e90d53d6795131"),
"createdTs" : ISODate("2015-09-24T13:15:36.609Z")
},
{
"_id" : ObjectId("5603fad216e90d53d6795132"),
"createdTs" : ISODate("2015-09-24T13:16:36.609Z")
}
],
"ok" : 1
}
Where am I wrong ?
The $group stage doesn't insure the ordering of the results. See here the first paragraph.
If you want the results to be sorted after a $group, you need to add a $sort after the $group stage.
In your case, you should move the $sort after the $group and before you ask the question : No, the $sort won't be able to use an index after the $group like it does before the $group :-).
The internal algorithm of $group seems to keep some sort of ordering (reversed apparently), but I would not count on that and add a $sort.
You are not doing anything wrong here, Its a $group behavior in Mongodb
Lets have a look in this example
Suppose you have following doc in collection
{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-01-01T08:00:00Z") }
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-02-03T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2014-02-03T09:05:00Z") }
{ "_id" : 4, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-02-15T08:00:00Z") }
{ "_id" : 5, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-02-15T09:05:00Z") }
{ "_id" : 6, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2014-02-15T12:05:10Z") }
{ "_id" : 7, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-02-15T14:12:12Z") }
Now if you run this
db.collection.aggregate([{ $sort: { item: 1,date:1}} ] )
the output will be in ascending order of item and date.
Now if you add group stage in aggregation pipeline it will reverse the order.
db.collection.aggregate([{ $sort: { item: 1,date:1}},{$group:{_id:"$item"}} ] )
Output will be
{ "_id" : "xyz" }
{ "_id" : "jkl" }
{ "_id" : "abc" }
Now the solution for your problem
change "createdTs": -1 to "createdTs": 1 for group

MongoDb query needs to be framed

I have a collection named 'Dealer' with following fields.
1) userId
2) dealerId
3) code
4) origin (having values 'UUM', 'MMT', 'TTC')
In the above collection, I need to get unique dealer records with unique combination of (userId + dealerId). If two records are having same 'userId' and 'dealerId' then I need to check the origin field and the record with value not equal to 'UUM' needs to be returned in resultset.
This is what I reached up to, as of now:
db.dealer.aggregate(
[
{
$sort: { origin: 1 }
},
{
$group:
{
_id: {
dealerId:"$dealerId",
recordId:"$recordId"
},
origin: {
$first: "$origin"
}
}
}
])
How can I make an aggregation query for the above scenario.Any help is appreciated.
Thanks in advance.
To reduce memory usage, I would filter the docs that have don't have a UUM origin.
Then the group by dealers and users.
I'm not sure why you're sorting and grouping by dealer + record.
Setup
> db.dealerTest.find()
{ "_id" : 1, "userId" : "u1", "dealerId" : "d1", "code" : 1, "origin" : "UUM" }
{ "_id" : 2, "userId" : "u1", "dealerId" : "d1", "code" : 2, "origin" : "UUM" }
{ "_id" : 3, "userId" : "u1", "dealerId" : "d1", "code" : 3, "origin" : "TTC" }
{ "_id" : 4, "userId" : "u2", "dealerId" : "d1", "code" : 4, "origin" : "TTC" }
{ "_id" : 5, "userId" : "u2", "dealerId" : "d1", "code" : 5, "origin" : "MMT" }
{ "_id" : 6, "userId" : "u2", "dealerId" : "d2", "code" : 6, "origin" : "UUM" }
Code:
db.dealerTest.aggregate([{
$match: {
origin: {
$ne: "UUM"
}
}
}, {
$group: {
_id: {
dealerId: "$dealerId",
userId: "$userId"
},
docIds: { $addToSet : "$_id" },
origins: { $addToSet : "$origin" }
}
}]);
Output:
{ "_id" : { "dealerId" : "d1", "userId" : "u2" }, "docIds" : [ 5, 4 ], "origins" : [ "MMT", "TTC" ] }
{ "_id" : { "dealerId" : "d1", "userId" : "u1" }, "docIds" : [ 3 ], "origins" : [ "TTC" ] }

Do $sort works for sub array document

I have a collection which has a field of array kind. I want to sort on the basis of a field of sub-array but Mongo is not sorting the data.
My collection is:
{
"_id" : ObjectId("51f1fcc08188d3117c6da351"),
"cust_id" : "abc123",
"ord_date" : ISODate("2012-10-03T18:30:00Z"),
"status" : "A",
"price" : 25,
"items" : [{
"sku" : "ggg",
"qty" : 7,
"price" : 2.5
}, {
"sku" : "ppp",
"qty" : 5,
"price" : 2.5
}]
}
My Query is:
db.orders.aggregate([
{ "$unwind" : "$items"} ,
{ "$match" : { }} ,
{ "$group" : { "items" : { "$addToSet" : { "sku" : "$items.sku"}} , "_id" : { }}} ,
{ "$sort" : { "items.sku" : 1}} ,
{ "$project" : { "_id" : 0 , "items" : 1}}
])
Result is:
"result" : [
{
"items" : [
{
"sku" : "ppp"
},
{
"sku" : "ggg"
}
]
}
],
"ok" : 1
}
Whereas "sku":"ggg" should come first when it is ascending.
You weant to do the sort BEFORE you regroup:
db.orders.aggregate([
{ "$unwind" : "$items"} ,
{ "$sort" : { "items.sku" : 1}},
{ "$match" : { }} ,
{ "$group" : { "items" : { "$push" : { "sku" : "$items.sku"}} , "_id" : null}} ,
{ "$project" : { "_id" : 0 , "items" : 1}}
])