Mongoose cant fetch all objects from array
up vote
0
down vote
favorite
I have a mongoose query that should fetch all Participants in certain Event model, but I only get back one participant. I tried the $all
method, still no result.
query:
Participants.find({'_id': {$all: event.participants}}, (err, participants) => {
if (err) {
return console.log(err);
}
console.log(participants);
});
output:
[ { createdAt: 1970-01-01T00:00:00.011Z,
_id: 5be7fc340016817bcb4ybdfc,
name: 'TEST',
ticket: 5be7fc340016817bc44fbdfb,
email: 'test@test.com',
__v: 0 } ]
expected output
[ { createdAt: 1970-01-01T00:00:00.011Z,
_id: 5be7fc340016817bcb4fbdfc,
name: 'TEST',
ticket: 5be7fc3400d16817bcb4fbdfb,
email: 'rene370d@gmail.com',
__v: 0 },
{ createdAt: 1970-01-01T00:00:00.011Z,
_id: 5be7fc340016817bcb4fbdfc,
name: 'Another test',
ticket: 5be7fc3s40016817bcb4fbdfb,
email: 'another@emai.com',
__v: 0 } ]
event.participants
participants:{
type: [mongoose.Schema.Types.ObjectId],
ref: 'ParticipantSchema'
}
node.js mongoose
add a comment |
up vote
0
down vote
favorite
I have a mongoose query that should fetch all Participants in certain Event model, but I only get back one participant. I tried the $all
method, still no result.
query:
Participants.find({'_id': {$all: event.participants}}, (err, participants) => {
if (err) {
return console.log(err);
}
console.log(participants);
});
output:
[ { createdAt: 1970-01-01T00:00:00.011Z,
_id: 5be7fc340016817bcb4ybdfc,
name: 'TEST',
ticket: 5be7fc340016817bc44fbdfb,
email: 'test@test.com',
__v: 0 } ]
expected output
[ { createdAt: 1970-01-01T00:00:00.011Z,
_id: 5be7fc340016817bcb4fbdfc,
name: 'TEST',
ticket: 5be7fc3400d16817bcb4fbdfb,
email: 'rene370d@gmail.com',
__v: 0 },
{ createdAt: 1970-01-01T00:00:00.011Z,
_id: 5be7fc340016817bcb4fbdfc,
name: 'Another test',
ticket: 5be7fc3s40016817bcb4fbdfb,
email: 'another@emai.com',
__v: 0 } ]
event.participants
participants:{
type: [mongoose.Schema.Types.ObjectId],
ref: 'ParticipantSchema'
}
node.js mongoose
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a mongoose query that should fetch all Participants in certain Event model, but I only get back one participant. I tried the $all
method, still no result.
query:
Participants.find({'_id': {$all: event.participants}}, (err, participants) => {
if (err) {
return console.log(err);
}
console.log(participants);
});
output:
[ { createdAt: 1970-01-01T00:00:00.011Z,
_id: 5be7fc340016817bcb4ybdfc,
name: 'TEST',
ticket: 5be7fc340016817bc44fbdfb,
email: 'test@test.com',
__v: 0 } ]
expected output
[ { createdAt: 1970-01-01T00:00:00.011Z,
_id: 5be7fc340016817bcb4fbdfc,
name: 'TEST',
ticket: 5be7fc3400d16817bcb4fbdfb,
email: 'rene370d@gmail.com',
__v: 0 },
{ createdAt: 1970-01-01T00:00:00.011Z,
_id: 5be7fc340016817bcb4fbdfc,
name: 'Another test',
ticket: 5be7fc3s40016817bcb4fbdfb,
email: 'another@emai.com',
__v: 0 } ]
event.participants
participants:{
type: [mongoose.Schema.Types.ObjectId],
ref: 'ParticipantSchema'
}
node.js mongoose
I have a mongoose query that should fetch all Participants in certain Event model, but I only get back one participant. I tried the $all
method, still no result.
query:
Participants.find({'_id': {$all: event.participants}}, (err, participants) => {
if (err) {
return console.log(err);
}
console.log(participants);
});
output:
[ { createdAt: 1970-01-01T00:00:00.011Z,
_id: 5be7fc340016817bcb4ybdfc,
name: 'TEST',
ticket: 5be7fc340016817bc44fbdfb,
email: 'test@test.com',
__v: 0 } ]
expected output
[ { createdAt: 1970-01-01T00:00:00.011Z,
_id: 5be7fc340016817bcb4fbdfc,
name: 'TEST',
ticket: 5be7fc3400d16817bcb4fbdfb,
email: 'rene370d@gmail.com',
__v: 0 },
{ createdAt: 1970-01-01T00:00:00.011Z,
_id: 5be7fc340016817bcb4fbdfc,
name: 'Another test',
ticket: 5be7fc3s40016817bcb4fbdfb,
email: 'another@emai.com',
__v: 0 } ]
event.participants
participants:{
type: [mongoose.Schema.Types.ObjectId],
ref: 'ParticipantSchema'
}
node.js mongoose
node.js mongoose
edited Nov 11 at 10:26
asked Nov 11 at 10:02
articapps
54
54
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
From the documentation:
The $all operator selects the documents where the value of a field is an array that contains all the specified elements.
So it doesn't make much sense to use it to get all documents for a certain model.
Instead, use the $in
operator
Participants.find({ _id: { $in: event.participants } }, (err, participants) => {
if (err) {
return console.log(err);
}
console.log(participants);
});
this code gets all the participants for every event, I only would like to fetch for one certain event
– articapps
Nov 11 at 10:24
I see, I'll update my answer
– Christian
Nov 11 at 10:30
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
From the documentation:
The $all operator selects the documents where the value of a field is an array that contains all the specified elements.
So it doesn't make much sense to use it to get all documents for a certain model.
Instead, use the $in
operator
Participants.find({ _id: { $in: event.participants } }, (err, participants) => {
if (err) {
return console.log(err);
}
console.log(participants);
});
this code gets all the participants for every event, I only would like to fetch for one certain event
– articapps
Nov 11 at 10:24
I see, I'll update my answer
– Christian
Nov 11 at 10:30
add a comment |
up vote
0
down vote
accepted
From the documentation:
The $all operator selects the documents where the value of a field is an array that contains all the specified elements.
So it doesn't make much sense to use it to get all documents for a certain model.
Instead, use the $in
operator
Participants.find({ _id: { $in: event.participants } }, (err, participants) => {
if (err) {
return console.log(err);
}
console.log(participants);
});
this code gets all the participants for every event, I only would like to fetch for one certain event
– articapps
Nov 11 at 10:24
I see, I'll update my answer
– Christian
Nov 11 at 10:30
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
From the documentation:
The $all operator selects the documents where the value of a field is an array that contains all the specified elements.
So it doesn't make much sense to use it to get all documents for a certain model.
Instead, use the $in
operator
Participants.find({ _id: { $in: event.participants } }, (err, participants) => {
if (err) {
return console.log(err);
}
console.log(participants);
});
From the documentation:
The $all operator selects the documents where the value of a field is an array that contains all the specified elements.
So it doesn't make much sense to use it to get all documents for a certain model.
Instead, use the $in
operator
Participants.find({ _id: { $in: event.participants } }, (err, participants) => {
if (err) {
return console.log(err);
}
console.log(participants);
});
edited Nov 11 at 10:31
answered Nov 11 at 10:18
Christian
2,99612544
2,99612544
this code gets all the participants for every event, I only would like to fetch for one certain event
– articapps
Nov 11 at 10:24
I see, I'll update my answer
– Christian
Nov 11 at 10:30
add a comment |
this code gets all the participants for every event, I only would like to fetch for one certain event
– articapps
Nov 11 at 10:24
I see, I'll update my answer
– Christian
Nov 11 at 10:30
this code gets all the participants for every event, I only would like to fetch for one certain event
– articapps
Nov 11 at 10:24
this code gets all the participants for every event, I only would like to fetch for one certain event
– articapps
Nov 11 at 10:24
I see, I'll update my answer
– Christian
Nov 11 at 10:30
I see, I'll update my answer
– Christian
Nov 11 at 10:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247611%2fmongoose-cant-fetch-all-objects-from-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown