MongoDB: $lookup returns empty array [duplicate]











up vote
4
down vote

favorite













This question already has an answer here:




  • Mongoose always returning an empty array NodeJS

    1 answer



  • Querying after populate in Mongoose

    6 answers




I have two models, user and schedule, and i want to combine these two using $lookup and mongoose.



User (model)



    name: {
type: String,
required: true
},
firstName: {
String
},
lastName: {
String
},
storeKey: {
type: String,
required: true
},
avatar: String,
birthday: String,
phone: {
type: String
},
doc: String,
email: {
type: String
},
password: {
passwordHash: String,
salt: String
},
active: {
type: Boolean,
default: true
},
deleted: {
type: Boolean,
default: false
},
generalObservations: {
type: String
},
from: {
type: String
},
services: {
type: Number,
default: 0
},
no_shows: {
type: Number,
default: 0
},
// campos para integração
integrationId: String
}, {
timestamps: true


Schedule (model)



 store: {
type: String,
required: true
},
customer: {
id: {
type: ObjectId
},
name: {
type: String,
required: true
},
avatar: String,
phone: {
type: String
},
doc: {
type: String
},
},
employee: {
id: {
type: String,
required: true
},
name: {
type: String,
required: true
},
avatar: String,
},
service: {
id: {
type: String
},
name: {
type: String,
required: true
},
filters: [String]
},
info: {
channel: {
type: String,
required: true,
default: 'app'
},
id: String,
name: String
},
scheduleDate: {
type: String,
required: true
},
scheduleStart: {
type: String,
required: true
},
scheduleEnd: {
type: String,
required: true
},
value: {
type: Number,
required: true
},
comissionType: {
type: String,
default: '$'
},
comissionValue: {
type: Number,
default: 0
},
status: {
type: Number,
required: true
},
observation: String,
paymentMethod: {
type: Number,
default: 0
},
paymentValue: String,
paymentChange: String
}, {
timestamps: {
createdAt: 'created',
updatedAt: 'updated'
}


And now my query using mongoose:



User aggregate



  User.aggregate([{
$match: {
storeKey: req.body.store,
}
},
{
$group: {
_id: {
id: "$_id",
name: "$name",
cpf: "$cpf",
phone: "$phone",
email: "$email",
birthday: "$birthday"
},
totalServices: {
$sum: "$services"
}
}
},
{
$lookup: {
from: "schedule",
localField: "_id.id",
foreignField: "customer.id",
as: "user_detail"
}
}


the result of my query is a empty array(user_detail) like this:



Result of the query:



{
"_id": {
"id": "5bdb5b16ee9b7a4aa810bc62",
"name": "Jonas com aniversário",
"phone": "11984798494",
"email": "j@jz.com",
"birthday": "Thu Nov 01 2018 16:59:18 GMT-0300 (Hora oficial do Brasil)"
},
"totalServices": 0,
"user_detail":
}


I don't know why, but the query result is a empty array and i was trying to using $unwind and $match but doesn't work too.



EDIT:



Collection of user



{
"_id": "5b1b1dcce1ab9a12a8eb580f",
"password": {
"salt": "d095f2",
"passwordHash": "b24881ef4c43d28e93bcff5da2ce32e4287aabf77540d2465482a435a5929a63f2ba9fb7e1cc14fa4e8183d83e33854ec6153fbbb872e65a9e3f188892bf56cc"
},
"name": "Anderson Zanardi",
"cpf": "31933765828",
"phone": "11996370565",
"birthday": "1984-03-18",
"email": "dev@wabiz.com.br",
"storeKey": "5b16cceb56a44e2f6cd0324b",
"createdAt": "2018-06-09T00:22:36.464Z",
"updatedAt": "2018-11-06T13:51:37.261Z",
"__v": 0,
"doc": "31933765828",
"active": true,
"from": "app",
"deleted": false,
"services": 80
},


Collection of schedule



{
"_id": "5b1c20d8fc76f904849712c9",
"customer": {
"id": "789456",
"name": "Gabriel Barreto",
"phone": "11995274098",
"cpf": "40735255814"
},
"employee": {
"id": "5b16cebd29bcf613f02b6fb4",
"name": "Anderson Zanardi",
"avatar": ""
},
"service": {
"filters": [
"corte_simples",
"corte_masculino"
],
"id": "service_id",
"name": "Corte Masculino"
},
"store": "5b16cceb56a44e2f6cd0324b",
"scheduleDate": "2018-06-07",
"scheduleStart": "2018-06-28 13:00",
"scheduleEnd": "2018-06-28 13:30",
"status": 1,
"value": 50,
"created": "2018-06-09T18:47:52.862Z",
"updated": "2018-06-09T18:47:52.862Z",
"__v": 0
},









share|improve this question















marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 20:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    Because your customer.id is of String type and the _id.id is of ObjectId. And that's why it doesn't match both and returns empty array.
    – Anthony Winzlet
    Nov 12 at 16:33










  • Can I convert that ObjectId to use this fields to match and do the $lookup?
    – Matheus
    Nov 12 at 16:39






  • 1




    It is working for me with the phone. See mongoplayground.net/p/s5_-g-rP63U
    – Anthony Winzlet
    Nov 12 at 16:58






  • 1




    Then probably the problem is somewhere else. I have shown you a working example.
    – Anthony Winzlet
    Nov 12 at 17:11






  • 1




    I change the field from: "schedule", to from: "schedules", and works!
    – Matheus
    Nov 12 at 17:17















up vote
4
down vote

favorite













This question already has an answer here:




  • Mongoose always returning an empty array NodeJS

    1 answer



  • Querying after populate in Mongoose

    6 answers




I have two models, user and schedule, and i want to combine these two using $lookup and mongoose.



User (model)



    name: {
type: String,
required: true
},
firstName: {
String
},
lastName: {
String
},
storeKey: {
type: String,
required: true
},
avatar: String,
birthday: String,
phone: {
type: String
},
doc: String,
email: {
type: String
},
password: {
passwordHash: String,
salt: String
},
active: {
type: Boolean,
default: true
},
deleted: {
type: Boolean,
default: false
},
generalObservations: {
type: String
},
from: {
type: String
},
services: {
type: Number,
default: 0
},
no_shows: {
type: Number,
default: 0
},
// campos para integração
integrationId: String
}, {
timestamps: true


Schedule (model)



 store: {
type: String,
required: true
},
customer: {
id: {
type: ObjectId
},
name: {
type: String,
required: true
},
avatar: String,
phone: {
type: String
},
doc: {
type: String
},
},
employee: {
id: {
type: String,
required: true
},
name: {
type: String,
required: true
},
avatar: String,
},
service: {
id: {
type: String
},
name: {
type: String,
required: true
},
filters: [String]
},
info: {
channel: {
type: String,
required: true,
default: 'app'
},
id: String,
name: String
},
scheduleDate: {
type: String,
required: true
},
scheduleStart: {
type: String,
required: true
},
scheduleEnd: {
type: String,
required: true
},
value: {
type: Number,
required: true
},
comissionType: {
type: String,
default: '$'
},
comissionValue: {
type: Number,
default: 0
},
status: {
type: Number,
required: true
},
observation: String,
paymentMethod: {
type: Number,
default: 0
},
paymentValue: String,
paymentChange: String
}, {
timestamps: {
createdAt: 'created',
updatedAt: 'updated'
}


And now my query using mongoose:



User aggregate



  User.aggregate([{
$match: {
storeKey: req.body.store,
}
},
{
$group: {
_id: {
id: "$_id",
name: "$name",
cpf: "$cpf",
phone: "$phone",
email: "$email",
birthday: "$birthday"
},
totalServices: {
$sum: "$services"
}
}
},
{
$lookup: {
from: "schedule",
localField: "_id.id",
foreignField: "customer.id",
as: "user_detail"
}
}


the result of my query is a empty array(user_detail) like this:



Result of the query:



{
"_id": {
"id": "5bdb5b16ee9b7a4aa810bc62",
"name": "Jonas com aniversário",
"phone": "11984798494",
"email": "j@jz.com",
"birthday": "Thu Nov 01 2018 16:59:18 GMT-0300 (Hora oficial do Brasil)"
},
"totalServices": 0,
"user_detail":
}


I don't know why, but the query result is a empty array and i was trying to using $unwind and $match but doesn't work too.



EDIT:



Collection of user



{
"_id": "5b1b1dcce1ab9a12a8eb580f",
"password": {
"salt": "d095f2",
"passwordHash": "b24881ef4c43d28e93bcff5da2ce32e4287aabf77540d2465482a435a5929a63f2ba9fb7e1cc14fa4e8183d83e33854ec6153fbbb872e65a9e3f188892bf56cc"
},
"name": "Anderson Zanardi",
"cpf": "31933765828",
"phone": "11996370565",
"birthday": "1984-03-18",
"email": "dev@wabiz.com.br",
"storeKey": "5b16cceb56a44e2f6cd0324b",
"createdAt": "2018-06-09T00:22:36.464Z",
"updatedAt": "2018-11-06T13:51:37.261Z",
"__v": 0,
"doc": "31933765828",
"active": true,
"from": "app",
"deleted": false,
"services": 80
},


Collection of schedule



{
"_id": "5b1c20d8fc76f904849712c9",
"customer": {
"id": "789456",
"name": "Gabriel Barreto",
"phone": "11995274098",
"cpf": "40735255814"
},
"employee": {
"id": "5b16cebd29bcf613f02b6fb4",
"name": "Anderson Zanardi",
"avatar": ""
},
"service": {
"filters": [
"corte_simples",
"corte_masculino"
],
"id": "service_id",
"name": "Corte Masculino"
},
"store": "5b16cceb56a44e2f6cd0324b",
"scheduleDate": "2018-06-07",
"scheduleStart": "2018-06-28 13:00",
"scheduleEnd": "2018-06-28 13:30",
"status": 1,
"value": 50,
"created": "2018-06-09T18:47:52.862Z",
"updated": "2018-06-09T18:47:52.862Z",
"__v": 0
},









share|improve this question















marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 20:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    Because your customer.id is of String type and the _id.id is of ObjectId. And that's why it doesn't match both and returns empty array.
    – Anthony Winzlet
    Nov 12 at 16:33










  • Can I convert that ObjectId to use this fields to match and do the $lookup?
    – Matheus
    Nov 12 at 16:39






  • 1




    It is working for me with the phone. See mongoplayground.net/p/s5_-g-rP63U
    – Anthony Winzlet
    Nov 12 at 16:58






  • 1




    Then probably the problem is somewhere else. I have shown you a working example.
    – Anthony Winzlet
    Nov 12 at 17:11






  • 1




    I change the field from: "schedule", to from: "schedules", and works!
    – Matheus
    Nov 12 at 17:17













up vote
4
down vote

favorite









up vote
4
down vote

favorite












This question already has an answer here:




  • Mongoose always returning an empty array NodeJS

    1 answer



  • Querying after populate in Mongoose

    6 answers




I have two models, user and schedule, and i want to combine these two using $lookup and mongoose.



User (model)



    name: {
type: String,
required: true
},
firstName: {
String
},
lastName: {
String
},
storeKey: {
type: String,
required: true
},
avatar: String,
birthday: String,
phone: {
type: String
},
doc: String,
email: {
type: String
},
password: {
passwordHash: String,
salt: String
},
active: {
type: Boolean,
default: true
},
deleted: {
type: Boolean,
default: false
},
generalObservations: {
type: String
},
from: {
type: String
},
services: {
type: Number,
default: 0
},
no_shows: {
type: Number,
default: 0
},
// campos para integração
integrationId: String
}, {
timestamps: true


Schedule (model)



 store: {
type: String,
required: true
},
customer: {
id: {
type: ObjectId
},
name: {
type: String,
required: true
},
avatar: String,
phone: {
type: String
},
doc: {
type: String
},
},
employee: {
id: {
type: String,
required: true
},
name: {
type: String,
required: true
},
avatar: String,
},
service: {
id: {
type: String
},
name: {
type: String,
required: true
},
filters: [String]
},
info: {
channel: {
type: String,
required: true,
default: 'app'
},
id: String,
name: String
},
scheduleDate: {
type: String,
required: true
},
scheduleStart: {
type: String,
required: true
},
scheduleEnd: {
type: String,
required: true
},
value: {
type: Number,
required: true
},
comissionType: {
type: String,
default: '$'
},
comissionValue: {
type: Number,
default: 0
},
status: {
type: Number,
required: true
},
observation: String,
paymentMethod: {
type: Number,
default: 0
},
paymentValue: String,
paymentChange: String
}, {
timestamps: {
createdAt: 'created',
updatedAt: 'updated'
}


And now my query using mongoose:



User aggregate



  User.aggregate([{
$match: {
storeKey: req.body.store,
}
},
{
$group: {
_id: {
id: "$_id",
name: "$name",
cpf: "$cpf",
phone: "$phone",
email: "$email",
birthday: "$birthday"
},
totalServices: {
$sum: "$services"
}
}
},
{
$lookup: {
from: "schedule",
localField: "_id.id",
foreignField: "customer.id",
as: "user_detail"
}
}


the result of my query is a empty array(user_detail) like this:



Result of the query:



{
"_id": {
"id": "5bdb5b16ee9b7a4aa810bc62",
"name": "Jonas com aniversário",
"phone": "11984798494",
"email": "j@jz.com",
"birthday": "Thu Nov 01 2018 16:59:18 GMT-0300 (Hora oficial do Brasil)"
},
"totalServices": 0,
"user_detail":
}


I don't know why, but the query result is a empty array and i was trying to using $unwind and $match but doesn't work too.



EDIT:



Collection of user



{
"_id": "5b1b1dcce1ab9a12a8eb580f",
"password": {
"salt": "d095f2",
"passwordHash": "b24881ef4c43d28e93bcff5da2ce32e4287aabf77540d2465482a435a5929a63f2ba9fb7e1cc14fa4e8183d83e33854ec6153fbbb872e65a9e3f188892bf56cc"
},
"name": "Anderson Zanardi",
"cpf": "31933765828",
"phone": "11996370565",
"birthday": "1984-03-18",
"email": "dev@wabiz.com.br",
"storeKey": "5b16cceb56a44e2f6cd0324b",
"createdAt": "2018-06-09T00:22:36.464Z",
"updatedAt": "2018-11-06T13:51:37.261Z",
"__v": 0,
"doc": "31933765828",
"active": true,
"from": "app",
"deleted": false,
"services": 80
},


Collection of schedule



{
"_id": "5b1c20d8fc76f904849712c9",
"customer": {
"id": "789456",
"name": "Gabriel Barreto",
"phone": "11995274098",
"cpf": "40735255814"
},
"employee": {
"id": "5b16cebd29bcf613f02b6fb4",
"name": "Anderson Zanardi",
"avatar": ""
},
"service": {
"filters": [
"corte_simples",
"corte_masculino"
],
"id": "service_id",
"name": "Corte Masculino"
},
"store": "5b16cceb56a44e2f6cd0324b",
"scheduleDate": "2018-06-07",
"scheduleStart": "2018-06-28 13:00",
"scheduleEnd": "2018-06-28 13:30",
"status": 1,
"value": 50,
"created": "2018-06-09T18:47:52.862Z",
"updated": "2018-06-09T18:47:52.862Z",
"__v": 0
},









share|improve this question
















This question already has an answer here:




  • Mongoose always returning an empty array NodeJS

    1 answer



  • Querying after populate in Mongoose

    6 answers




I have two models, user and schedule, and i want to combine these two using $lookup and mongoose.



User (model)



    name: {
type: String,
required: true
},
firstName: {
String
},
lastName: {
String
},
storeKey: {
type: String,
required: true
},
avatar: String,
birthday: String,
phone: {
type: String
},
doc: String,
email: {
type: String
},
password: {
passwordHash: String,
salt: String
},
active: {
type: Boolean,
default: true
},
deleted: {
type: Boolean,
default: false
},
generalObservations: {
type: String
},
from: {
type: String
},
services: {
type: Number,
default: 0
},
no_shows: {
type: Number,
default: 0
},
// campos para integração
integrationId: String
}, {
timestamps: true


Schedule (model)



 store: {
type: String,
required: true
},
customer: {
id: {
type: ObjectId
},
name: {
type: String,
required: true
},
avatar: String,
phone: {
type: String
},
doc: {
type: String
},
},
employee: {
id: {
type: String,
required: true
},
name: {
type: String,
required: true
},
avatar: String,
},
service: {
id: {
type: String
},
name: {
type: String,
required: true
},
filters: [String]
},
info: {
channel: {
type: String,
required: true,
default: 'app'
},
id: String,
name: String
},
scheduleDate: {
type: String,
required: true
},
scheduleStart: {
type: String,
required: true
},
scheduleEnd: {
type: String,
required: true
},
value: {
type: Number,
required: true
},
comissionType: {
type: String,
default: '$'
},
comissionValue: {
type: Number,
default: 0
},
status: {
type: Number,
required: true
},
observation: String,
paymentMethod: {
type: Number,
default: 0
},
paymentValue: String,
paymentChange: String
}, {
timestamps: {
createdAt: 'created',
updatedAt: 'updated'
}


And now my query using mongoose:



User aggregate



  User.aggregate([{
$match: {
storeKey: req.body.store,
}
},
{
$group: {
_id: {
id: "$_id",
name: "$name",
cpf: "$cpf",
phone: "$phone",
email: "$email",
birthday: "$birthday"
},
totalServices: {
$sum: "$services"
}
}
},
{
$lookup: {
from: "schedule",
localField: "_id.id",
foreignField: "customer.id",
as: "user_detail"
}
}


the result of my query is a empty array(user_detail) like this:



Result of the query:



{
"_id": {
"id": "5bdb5b16ee9b7a4aa810bc62",
"name": "Jonas com aniversário",
"phone": "11984798494",
"email": "j@jz.com",
"birthday": "Thu Nov 01 2018 16:59:18 GMT-0300 (Hora oficial do Brasil)"
},
"totalServices": 0,
"user_detail":
}


I don't know why, but the query result is a empty array and i was trying to using $unwind and $match but doesn't work too.



EDIT:



Collection of user



{
"_id": "5b1b1dcce1ab9a12a8eb580f",
"password": {
"salt": "d095f2",
"passwordHash": "b24881ef4c43d28e93bcff5da2ce32e4287aabf77540d2465482a435a5929a63f2ba9fb7e1cc14fa4e8183d83e33854ec6153fbbb872e65a9e3f188892bf56cc"
},
"name": "Anderson Zanardi",
"cpf": "31933765828",
"phone": "11996370565",
"birthday": "1984-03-18",
"email": "dev@wabiz.com.br",
"storeKey": "5b16cceb56a44e2f6cd0324b",
"createdAt": "2018-06-09T00:22:36.464Z",
"updatedAt": "2018-11-06T13:51:37.261Z",
"__v": 0,
"doc": "31933765828",
"active": true,
"from": "app",
"deleted": false,
"services": 80
},


Collection of schedule



{
"_id": "5b1c20d8fc76f904849712c9",
"customer": {
"id": "789456",
"name": "Gabriel Barreto",
"phone": "11995274098",
"cpf": "40735255814"
},
"employee": {
"id": "5b16cebd29bcf613f02b6fb4",
"name": "Anderson Zanardi",
"avatar": ""
},
"service": {
"filters": [
"corte_simples",
"corte_masculino"
],
"id": "service_id",
"name": "Corte Masculino"
},
"store": "5b16cceb56a44e2f6cd0324b",
"scheduleDate": "2018-06-07",
"scheduleStart": "2018-06-28 13:00",
"scheduleEnd": "2018-06-28 13:30",
"status": 1,
"value": 50,
"created": "2018-06-09T18:47:52.862Z",
"updated": "2018-06-09T18:47:52.862Z",
"__v": 0
},




This question already has an answer here:




  • Mongoose always returning an empty array NodeJS

    1 answer



  • Querying after populate in Mongoose

    6 answers








node.js mongodb mongoose mongodb-query aggregation-framework






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 18:18









Anthony Winzlet

13k41139




13k41139










asked Nov 12 at 16:29









Matheus

381216




381216




marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 20:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 20:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    Because your customer.id is of String type and the _id.id is of ObjectId. And that's why it doesn't match both and returns empty array.
    – Anthony Winzlet
    Nov 12 at 16:33










  • Can I convert that ObjectId to use this fields to match and do the $lookup?
    – Matheus
    Nov 12 at 16:39






  • 1




    It is working for me with the phone. See mongoplayground.net/p/s5_-g-rP63U
    – Anthony Winzlet
    Nov 12 at 16:58






  • 1




    Then probably the problem is somewhere else. I have shown you a working example.
    – Anthony Winzlet
    Nov 12 at 17:11






  • 1




    I change the field from: "schedule", to from: "schedules", and works!
    – Matheus
    Nov 12 at 17:17














  • 1




    Because your customer.id is of String type and the _id.id is of ObjectId. And that's why it doesn't match both and returns empty array.
    – Anthony Winzlet
    Nov 12 at 16:33










  • Can I convert that ObjectId to use this fields to match and do the $lookup?
    – Matheus
    Nov 12 at 16:39






  • 1




    It is working for me with the phone. See mongoplayground.net/p/s5_-g-rP63U
    – Anthony Winzlet
    Nov 12 at 16:58






  • 1




    Then probably the problem is somewhere else. I have shown you a working example.
    – Anthony Winzlet
    Nov 12 at 17:11






  • 1




    I change the field from: "schedule", to from: "schedules", and works!
    – Matheus
    Nov 12 at 17:17








1




1




Because your customer.id is of String type and the _id.id is of ObjectId. And that's why it doesn't match both and returns empty array.
– Anthony Winzlet
Nov 12 at 16:33




Because your customer.id is of String type and the _id.id is of ObjectId. And that's why it doesn't match both and returns empty array.
– Anthony Winzlet
Nov 12 at 16:33












Can I convert that ObjectId to use this fields to match and do the $lookup?
– Matheus
Nov 12 at 16:39




Can I convert that ObjectId to use this fields to match and do the $lookup?
– Matheus
Nov 12 at 16:39




1




1




It is working for me with the phone. See mongoplayground.net/p/s5_-g-rP63U
– Anthony Winzlet
Nov 12 at 16:58




It is working for me with the phone. See mongoplayground.net/p/s5_-g-rP63U
– Anthony Winzlet
Nov 12 at 16:58




1




1




Then probably the problem is somewhere else. I have shown you a working example.
– Anthony Winzlet
Nov 12 at 17:11




Then probably the problem is somewhere else. I have shown you a working example.
– Anthony Winzlet
Nov 12 at 17:11




1




1




I change the field from: "schedule", to from: "schedules", and works!
– Matheus
Nov 12 at 17:17




I change the field from: "schedule", to from: "schedules", and works!
– Matheus
Nov 12 at 17:17












1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










Mongoose pluralize the collection name at the time of creation. So instead of schedule you should use schedules



{ "$lookup": {
"from": "schedules",
"localField": "_id.id",
"foreignField": "customer.id",
"as": "user_detail"
}}


or either import the collection and extract the collection name from it



const Schedule = require('/schedules')

{ "$lookup": {
"from": Schedule.collection.name,
"localField": "_id.phone",
"foreignField": "customer.phone",
"as": "user_detail"
}}





share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    Mongoose pluralize the collection name at the time of creation. So instead of schedule you should use schedules



    { "$lookup": {
    "from": "schedules",
    "localField": "_id.id",
    "foreignField": "customer.id",
    "as": "user_detail"
    }}


    or either import the collection and extract the collection name from it



    const Schedule = require('/schedules')

    { "$lookup": {
    "from": Schedule.collection.name,
    "localField": "_id.phone",
    "foreignField": "customer.phone",
    "as": "user_detail"
    }}





    share|improve this answer

























      up vote
      3
      down vote



      accepted










      Mongoose pluralize the collection name at the time of creation. So instead of schedule you should use schedules



      { "$lookup": {
      "from": "schedules",
      "localField": "_id.id",
      "foreignField": "customer.id",
      "as": "user_detail"
      }}


      or either import the collection and extract the collection name from it



      const Schedule = require('/schedules')

      { "$lookup": {
      "from": Schedule.collection.name,
      "localField": "_id.phone",
      "foreignField": "customer.phone",
      "as": "user_detail"
      }}





      share|improve this answer























        up vote
        3
        down vote



        accepted







        up vote
        3
        down vote



        accepted






        Mongoose pluralize the collection name at the time of creation. So instead of schedule you should use schedules



        { "$lookup": {
        "from": "schedules",
        "localField": "_id.id",
        "foreignField": "customer.id",
        "as": "user_detail"
        }}


        or either import the collection and extract the collection name from it



        const Schedule = require('/schedules')

        { "$lookup": {
        "from": Schedule.collection.name,
        "localField": "_id.phone",
        "foreignField": "customer.phone",
        "as": "user_detail"
        }}





        share|improve this answer












        Mongoose pluralize the collection name at the time of creation. So instead of schedule you should use schedules



        { "$lookup": {
        "from": "schedules",
        "localField": "_id.id",
        "foreignField": "customer.id",
        "as": "user_detail"
        }}


        or either import the collection and extract the collection name from it



        const Schedule = require('/schedules')

        { "$lookup": {
        "from": Schedule.collection.name,
        "localField": "_id.phone",
        "foreignField": "customer.phone",
        "as": "user_detail"
        }}






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 17:38









        Anthony Winzlet

        13k41139




        13k41139















            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)