I have user collection having data like this
{
"_id" : ObjectId("5da594c15324fec81d000027"),
"password" : "******",
"activation" : "Active",
"userType" : "Author",
"email" : "something#gmail.com",
"name" : "Something",
"profilePicture" : "profile_pictures/5da594c15324fec81d0000271607094354423image.png",
"__v" : 0
}
On the other hand userlog has data like this
{
"_id" : ObjectId("5fcb7bb4485c34a41900002b"),
"duration" : 2.54,
"page" : 1,
"activityDetails" : "Viewed Page for seconds",
"contentType" : "article",
"activityType" : "articlePageStayTime",
"bookId" : ObjectId("5f93e2cc74153f8c1800003f"),
"ipAddress" : "::1",
"creator" : ObjectId("5da594c15324fec81d000027"),
"created" : ISODate("2020-12-05T12:23:16.867Z"),
"__v" : 0
}
What I need is data like below
{
"_id" : ObjectId("5da594c15324fec81d000027"),
"password" : "******",
"activation" : "Active",
"userType" : "Author",
"email" : "something#gmail.com",
"name" : "Something",
"profilePicture" : "profile_pictures/5da594c15324fec81d0000271607094354423image.png",
"userlogs":
[{
"_id" : ObjectId("5fcb7bb4485c34a41900002b"),
"duration" : 2.54,
"page" : 1,
"activityDetails" : "Viewed Page for seconds",
"contentType" : "article",
"activityType" : "articlePageStayTime",
"bookId" : ObjectId("5f93e2cc74153f8c1800003f"),
"ipAddress" : "::1",
"creator" : ObjectId("5da594c15324fec81d000027"),
"created" : ISODate("2020-12-05T12:23:16.867Z"),
"__v" : 0
}]
}
I am trying to find all the user except admin with their log for each month. So my condition is user wont be admin and date will be between two range. But it is not working. My current code is below which is returning empty dataset-
User
.aggregate([
{
$match: {
userType: {
$ne:"admin"
}
},
"$and": [
{
"userlogs.created": {
$lte: dateCompare.end
}
},
{
"userlogs.created": {
$gte: dateCompare.start
}
}
]
},
{
$lookup:{
from: "userlogs", //or Races.collection.name
localField: "_id",
foreignField: "creator",
as: "userlogs"
},
},
]
I am using mongodb version 3.2
Try this
User.aggregate([
{
$match: {
userType: {
$ne:"admin"
}
},
{
$graphLookup:{
from: "userlogs", //or Races.collection.name
startWith: "$_id",
connectToField:"creator",
connectFromField:"_id",
maxDepth:0,
as: "userlogs",
restrictSearchWithMatch:{created:{
$lte:dateCompare.end,
$gte:dateCompare.start
}}
},
} ]
Related
I have 2 collections,1st is:
{
"_id" : ObjectId("62eb5713ac2dccfb0a75d6c0"),
"title" : "Agile Web Development with Rails",
"categoryId" : ObjectId("62eb5713ac2dccfb0a75d6bf"),
"subtitle" : "Dive into ES6 and the Future of JavaScript",
"author" : "Sam Ruby, Dave Thomas, David Heinemeier Hansson",
"published" : 2010,
"publisher" : "O'Reilly Media",
"isActive" : true,
"isDelete" : false,
"__v" : 0
}
{
"_id" : ObjectId("62eb5777ac2dccfb0a75d6c3"),
"title" : "Eloquent JavaScript, Third Edition",
"categoryId" : ObjectId("62eb5777ac2dccfb0a75d6c2"),
"subtitle" : "A Modern Introduction to Programming",
"author" : "Marijn Haverbeke",
"published" : 2018,
"publisher" : "No Starch Press",
"isActive" : true,
"isDelete" : false,
"__v" : 0
}
{
"_id" : ObjectId("62eb5aa0e45707fec304e115"),
"title" : "Eloquent JavaScript, Third Edition",
"categoryId" : ObjectId("62eb41f088b1bc88e8a416db"),
"subtitle" : "A Modern Introduction to Programming",
"author" : "Marijn Haverbeke",
"published" : 2018,
"publisher" : "No Starch Press",
"isActive" : true,
"isDelete" : false,
"__v" : 0
}
{
"_id" : ObjectId("62ebaf20de65e74cd055565a"),
"title" : "Rethinking Productivity in Software Engineering",
"categoryId" : ObjectId("62eb420b88b1bc88e8a416df"),
"subtitle" : "Everything you neeed to know about Gi",
"author" : "Caitlin Sadowski, Thomas Zimmermann",
"published" : 2019,
"publisher" : "Apress",
"isActive" : true,
"isDelete" : false,
"__v" : 0
}
{
"_id" : ObjectId("62f08a12e0346e06e4bb7b06"),
"categoryId" : ObjectId("62f08a12e0346e06e4bb7b05"),
"isActive" : true,
"isDelete" : false,
"__v" : 0
}
2nd collection is:
{
"_id" : ObjectId("62f3377166def37dee13f400"),
"book_id" : "62eb5713ac2dccfb0a75d6c0",
"description" : "for Agile Web..."
}
{
"_id" : ObjectId("62f3383566def37dee13f401"),
"book_id" : "62ebaf20de65e74cd055565a",
"description" : "for Rethinking Productivity..."
}
{
"_id" : ObjectId("62f3388666def37dee13f402"),
"book_id" : "62eb5aa0e45707fec304e115",
"description" : "for Eloquent JavaScript..."
}
I want to join 2 collections by use of aggregate $lookup,$match,$and,$ecpr,$eq and get the data in Mongo compass by using 'pipe line from text'. My code is:
[{$lookup:
({
from: "books",
let: {
"bkid": new mongoose.Types.ObjectId('book_id')
},
pipeline: [{
$match: {
$and: {
$expr: {
$eq: ['$_id', '$$bkid']
}
}
}
}],
as: "res"
})
}]
What is wrong here why its not showing output?
I am doing this code in Mongodbaggregation -> CREATE -> Pipeline for Text.
Thanks in advance.
It can be possible by two ways i.e,
1.by converting id type ObjectId to string
db.books.aggregate({
"$project": {
"_id": {
"$toString": "$_id"
}
}
},
{
"$lookup": {
"from": "description",
"localField": "_id",
"foreignField": "book_id",
"as": "result"
}
})
2.by converting string to ObjectId
db.description.aggregate([
{
$lookup: {
from: "books",
let: { bkid: {$convert: {input: '$book_id', to : 'objectId', onError: 'error',onNull: 'null'}} },
pipeline: [{
$match: {
$and: [
{ $expr: { $eq: ['$_id', '$$bkid'] } }
],
}
}],
as: "result"
}
}
])
I want to replace the external user ids with the real user properties after $lookup, my data:
"comments" : [
{
"user_Id" : ObjectId("aaa"),
"content" : "aaaa",
"rep" : [
{
"user_Id" : ObjectId("bbb"),
"comment" : "bbbb",
},
{
"user_Id" : ObjectId("ccc"),
"comment" : "cccc",
}
]
},
{
"user_Id" : ObjectId("ddd"),
"content" : "ddd",
"rep" : [ ]
}
]
User collection:
"users" : [
{
"_id" : ObjectId("aaa"),
"name" : "user1",
"email" : "test1#test.com",
},
{
"_id" : ObjectId("bbb"),
"username" : "user2",
"email" : "test2#test.com",
}
]
What i want to archieve:
"comments" : [
{
"user" : {
"_id" : ObjectId("aaa"),
"name" : "user1",
"email" : "test1#test.com",
}
"content" : "aaaa",
"rep" : [
{
"userId" : {
"_id" : ObjectId("bbb"),
"username" : "user2",
"email" : "test2#test.com",
},
"comment" : "bbbb",
},
{
"user" : {
"_id" : ObjectId("aaa"),
"name" : "user1",
"email" : "test1#test.com",
},
"comment" : "cccc",
}
]
},
{
"user" : {
"_id" : ObjectId("bbb"),
"username" : "user2",
"email" : "test2#test.com",
},
"content" : "ddd",
"rep" : [ ]
}
]
Right now i managed to get my user info from the external id but i'm going crazy trying to get the user object inside the replies too, i tried to group after grouping but nothing to do, this is what i did:
db.pages.aggregate([
{
$match: { _id: ObjectId('5db599f3fffdee1c822269e0b3') }
},
{
$project: {
comments: 1,
}
},
{ $unwind: '$comments' },
{
$lookup:
{
from: 'users',
localField: 'comments.user_Id',
foreignField: '_id',
as: 'us'
}
},
{ $unwind: '$us' },
{
$group: {
_id: {
user: {
id: '$us._id',
name: '$us.username',
email: '$us.email',
},
comments: {
comment: '$comments.comment',
rep: '$comments.rep'
},
}
}
}
]).pretty()
I am currently creating a request, but I can not.
So there is one the document of 1 user:
"_id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"search" : "flarize",
"name" : "flarize",
"email" : "flarize.a473#gmail.com",
"password" : "$2a$10$eYeOtEkEUyD7TFkjKvhZOuSSpvBolkL17TrPHuoHhOT8JrsQR0UKW",
"color" : 0,
"profil" : "",
"banner" : "",
"desc" : "",
"date" : 1540501286109,
"friend" : [
{
"id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"date" : 1540713424488
}
]
And my query:
db.users.aggregate([{
$match:{
search: "flarize"
}},{
$lookup:{
from: "users",
let:{friendId:"$friend.id"},
pipeline:[{
$match:{
$expr:{
$in:["$_id","$$friendId"]
}
}},{
$limit:10},{
$skip:0},{
$project: {
name: 1,
search:1,
desc:1,
friend:1,
date:1,
banner:1,
profil:1,
color: 1
}
}],
as:"friends"}},{
$project:{
profil:1,
search:1,
name:1,
profile:1,
banner:1,
color:1,
date:1,
desc:1,
friend:1,
friends:1
}
}]).pretty();
I got this result:
{
"_id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"search" : "flarize",
"name" : "flarize",
"color" : 0,
"profil" : "",
"banner" : "",
"desc" : "",
"date" : 1540501286109,
"friend" : [
{
"id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"date" : 1540713424488
}
],
"friends" : [
{
"_id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"search" : "flarize",
"name" : "flarize",
"color" : 0,
"profil" : "",
"banner" : "",
"desc" : "",
"date" : 1540501286109,
"friend" : [
{
"id" : ObjectId("5bd22f28f77cfb1f6ce503ca"),
"date" : 1540713424488
}
]
}
]
}
It's not really what I want.I would like Compare the document of the selection person, and the one who requests the query if the id of the person selected is present in friend then is_friend will be equal to true otherwise it will be equal to false.
I try this think:
db.users.aggregate([{
$match:{
search: "flarize"
}},{
$lookup:{
from: "users",
let:{friendId:"$friend.id"},
pipeline:[{
$match:{
$expr:{
$in:["$_id","$$friendId"]
}
}},{
$limit:10},{
$skip:0},{
$project: {
name: 1,
search:1,
desc:1,
friend:1,
date:1,
banner:1,
profil:1,
color: 1
is_friend:{
$lookup:{
from:"users",
let:{friendIdIs:"$_id"},
pipeline:[{
$match:{
$expr:{
$and:[{
$in:["$$friendIdIs", "$friend.is"]},{
$eq:["_id", ObjectId("5bd22f28f77cfb1f6ce503ca")]
}]
}
}
}],
as:"yes"}}
}
}
}],
as:"friends"}},{
$project:{
profil:1,
search:1,
name:1,
profile:1,
banner:1,
color:1,
date:1,
desc:1,
friend:1,
friends:1
}
}]).pretty();
But it's don't work.
Thank you for helping me
I tried to write a query in which collection 1 (User) consists of username,reporting manager,empId and collection two (quotation collection) The result i want is if logged in person is manager need to display the quotations created by there team and quotation created by themselves
User.aggregate([
{ $match: { reportingManager: req.user.user.name } },
{
$lookup:
{
from: 'Quotation',
localField: 'empId',
foreignField: 'salesCrm',
as: 'data'
}
},
{ $unwind: "$data" },
{$match:{"data.salesCrm":{$in:[req.user.user.empId]}}}
]
My user collection
{
"_id" : ObjectId("5b569cb4e924600e208660d6"),
"name" : "abc",
"email" : "abc#knowledgew.com",
"designation" : "Software Developer",
"department" : "Software Developer",
"empId" : "123",
"mobileNo" : 24564121654.0,
"password" : "$2a$10$5d/0XV.yq.w5.Ipwc2uvtuvK4Mxji0f1mUW8rBwlhu/QR8jpztoOu",
"vertical" : "Education,Market Research,Manufacturing,IT/ITES,BFSI",
"reportingManager" : "def",
"level" : "L1"
}
My quotation Collection
{
"_id" : ObjectId("5b4f27f476d65f25cc8d80ba"),
"clientId" : "VPKW00001",
"clientName" : "sssssss",
"quotationId" : "00001_1819_CG7JE",
"pmCost" : NumberInt(34),
"contactPerson" : "5b4310db728ebf030c447ebd",
"fileDomain" : "Panel Interview",
"clientDomain" : "Software",
"fuzzyMatch" : "No",
"fileEngineering" : NumberInt(34),
"workingDays" : "34",
"currency" : "GBP",
"scopeOfWork" : "LZ6PKLY_prod2.png",
"projectPlan" : "V1RK7BW_prod2.png",
"projectType" : "nnnn",
"projectValue" : 646.41,
"salesCrm" : "123",
}
Here is my actual database schema.
company_id is reference object of companies collection and booking_days.consultants.consultant_id is reference object of users collection.
I want to join embedded document with company_id and booking_days.consultants.consultant_id.
{
"_id" : ObjectId("5a7040d664544e1bb877deae"),
"company_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
"booking_days" : [
{
"booking_date" : ISODate("2018-01-31T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877deca"),
"consultants" : [
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"_id" : ObjectId("5a7040d664544e1bb877decc")
},
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52f"),
"_id" : ObjectId("5a7040d664544e1bb877decb")
}
]
},
{
"booking_date" : ISODate("2018-02-01T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877dec6"),
"consultants" : [
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52f"),
"_id" : ObjectId("5a7040d664544e1bb877dec9")
},
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"_id" : ObjectId("5a7040d664544e1bb877dec8")
},
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"_id" : ObjectId("5a7040d664544e1bb877dec7")
}
]
},
{
"booking_date" : ISODate("2018-02-02T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877dec4"),
"consultants" : [
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"_id" : ObjectId("5a7040d664544e1bb877dec5")
}
]
},
],
"__v" : 0
}
I am using below query.
db.getCollection('booking_days').aggregate(
[
{ $match: { company_id:ObjectId("5a6eb43f437e6a0d9e00c92f") } },
{
$lookup: {
localField: "company_id",
from: "companies",
foreignField: "_id",
as: "companies"
},
},
{
$lookup: {
localField: "booking_days.consultants.consultant_id",
from: "users",
foreignField: "_id",
as: "userssss"
},
},
{
$unwind:"$companies"
},
]
)
Actual Output
{
"_id" : ObjectId("5a7040d664544e1bb877deae"),
"company_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
"booking_days" : [
{
"booking_date" : ISODate("2018-01-31T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877deca"),
"consultants" : [
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"_id" : ObjectId("5a7040d664544e1bb877decc")
},
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52f"),
"_id" : ObjectId("5a7040d664544e1bb877decb")
}
]
},
{
"booking_date" : ISODate("2018-02-01T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877dec6"),
"consultants" : [
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52f"),
"_id" : ObjectId("5a7040d664544e1bb877dec9")
},
]
},
],
"__v" : 0,
"companies" : {
"_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
"first_name" : "Adrienne Runolfsson",
},
"users" : [
{
"_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"first_name" : "Christ Hamill",
},
{
"_id" : ObjectId("5a6f2854ce7d6938de1dd52e"),
"first_name" : "Miss Dina Kovacek",
},
]
}
Excepted output. consultant data will come in booking_days.consultants array.
{
"_id" : ObjectId("5a7040d664544e1bb877deae"),
"company_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
"booking_days" : [
{
"booking_date" : ISODate("2018-01-31T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877deca"),
"consultants" : [
{
"consultant_id" : {
"_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"first_name" : "Christ Hamill",
},
"_id" : ObjectId("5a7040d664544e1bb877decc")
},
{
"consultant_id" : {
"_id" : ObjectId("5a6f2854ce7d6938de1dd52e"),
"first_name" : "Miss Dina Kovacek",
},
"_id" : ObjectId("5a7040d664544e1bb877decb")
}
]
},
{
"booking_date" : ISODate("2018-02-01T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877dec6"),
"consultants" : [
{
"consultant_id" : {
"_id" : ObjectId("5a6f2854ce7d6938de1dd52e"),
"first_name" : "Miss Dina Kovacek",
},
"_id" : ObjectId("5a7040d664544e1bb877dec9")
},
]
},
],
"__v" : 0,
"companies" : {
"_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
"first_name" : "Adrienne Runolfsson",
},
}
As such you have to $unwind the localField when it is an embedded document array expect in some cases where localField is an array of scalar ids.
$unwind twice as consultant array is two levels deep followed by $lookup to get the name and $group to get back the expected output.
db.getCollection('booking_days').aggregate([
{"$match":{"company_id":ObjectId("5a6eb43f437e6a0d9e00c92f")}},
{"$lookup":{"localField":"company_id","from":"companies","foreignField":"_id","as":"companies"}},
{"$unwind":"$companies"},
{"$unwind":"$booking_days"},
{"$unwind":"$consultants"},
{"$lookup":{
"localField":"booking_days.consultants.consultant_id",
"from":"users",
"foreignField":"_id",
"as":"booking_days.consultants.consultant_id"
}},
{"$group":{
"_id":{"_id":"$_id","booking_days_id":"$booking_days._id"},
"company_id":{"$first":"$company_id"},
"booking_date":{"$first":"$booking_days.booking_date"},
"companies":{"$first":"$companies"},
"consultants":{"$push":"$booking_days.consultants"}
}},
{"$group":{
"_id":"$_id._id",
"company_id":{"$first":"$company_id"},
"companies":{"$first":"$companies"},
"booking_days":{
"$push":{
"_id":"$_id.booking_days_id",
"booking_date":"$booking_date",
"consultants":"$consultants"
}
}
}}
])
{"Id": "5b87a4c79a9c3feac943fc6c",
"comments" : [
{
"likes" : [],
"_id" : ObjectId("5b87a4c79a9c3feac943fc6c"),
"comment" : "string",
"accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
"commentId" : "7d2a05d1-2026-4a13-a5c1-318ed80d1b38",
"reply" : [
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string",
"accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
"replyId" : "ec220fd7-3440-44dc-9178-7a1183879463"
},
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string klllll",
"accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
"replyId" : "ec220fd7-3440-44dc-9178-7a1183879463"
}
]
},
{
"likes" : [],
"_id" : ObjectId("5b87c301c8a07efa2599c29e"),
"comment" : "testing",
"accountId" : "cfd29f53-d73e-480c-9cfa-ea42b4119266",
"commentId" : "0676047b-1712-4f70-89d5-29c1abe03eaf",
"reply" : [
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string",
"accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
"replyId" : "ec220fd7-3440-44dc-9178-7a1183879463"
},
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string klllll",
"accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
"replyId" : "ec220fd7-3440-44dc-9178-7a1183879463"
}
]
}
]
}
accountId is in differnt connection
// Expected Out Put
{"Id": "5b87a4c79a9c3feac943fc6c",
"comments" : [
{
"likes" : [],
"_id" : ObjectId("5b87a4c79a9c3feac943fc6c"),
"comment" : "string",
"name" : "apple",
"reply" : [
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string",
"name" : "apple",
},
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string klllll",
"name" : "apple",
}
]
},
{
"likes" : [],
"_id" : ObjectId("5b87c301c8a07efa2599c29e"),
"comment" : "testing",
"name" : "ball",
"reply" : [
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string",
"name" : "apple",
},
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string klllll",
"name" : "apple", }
]
}
]
}