node mongodb 3.4 using find with specify fields and toArray does not filter results [duplicate]
This question already has an answer here:
batchSize field name ignored in Field Projection
2 answers
Again, using mongo v3.4 and referencing these docs: https://docs.mongodb.com/v3.4/tutorial/project-fields-from-query-results/
My example looks like:
const m = this.getCollection(SOME_COLLECTION);
m.find({
'_id': {
$nin: [Ace, Bay],
},
'value.someCategory': {
$type: 'object',
},
}, {
'_id': 0,
'value.someCategory': 1,
}).toArray((err, doc) => {
if (err) {
console.log(err);
} else {
console.log(doc);
}
});
My doc
array will return all items that follow my filter of value.someCategory
of type object, but will not remove the _id
and will return all fields even though I'd like to specify only the value.someCategory
field.
Example data in mongo:
[
{
_id: 'hello',
value: {
someCategory: [Object],
name: 'hello',
otherCategory: true,
}
},
{
_id: 'Ace',
value: {
someCategory: [Object],
name: 'Ace',
otherCategory: true,
}
},
{
_id: 'testing',
value: {
someCategory: null,
name: 'testing',
otherCategory: true,
}
},
]
And expect the result to be:
[
{
someCategory: [Object],
},
]
Based on the docs linked above, the specified fields should be the second param. I'm wondering now if the use of toArray
affects the return value?
node.js mongodb mongodb-query
marked as duplicate by Neil Lunn
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 21 '18 at 19:58
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.
add a comment |
This question already has an answer here:
batchSize field name ignored in Field Projection
2 answers
Again, using mongo v3.4 and referencing these docs: https://docs.mongodb.com/v3.4/tutorial/project-fields-from-query-results/
My example looks like:
const m = this.getCollection(SOME_COLLECTION);
m.find({
'_id': {
$nin: [Ace, Bay],
},
'value.someCategory': {
$type: 'object',
},
}, {
'_id': 0,
'value.someCategory': 1,
}).toArray((err, doc) => {
if (err) {
console.log(err);
} else {
console.log(doc);
}
});
My doc
array will return all items that follow my filter of value.someCategory
of type object, but will not remove the _id
and will return all fields even though I'd like to specify only the value.someCategory
field.
Example data in mongo:
[
{
_id: 'hello',
value: {
someCategory: [Object],
name: 'hello',
otherCategory: true,
}
},
{
_id: 'Ace',
value: {
someCategory: [Object],
name: 'Ace',
otherCategory: true,
}
},
{
_id: 'testing',
value: {
someCategory: null,
name: 'testing',
otherCategory: true,
}
},
]
And expect the result to be:
[
{
someCategory: [Object],
},
]
Based on the docs linked above, the specified fields should be the second param. I'm wondering now if the use of toArray
affects the return value?
node.js mongodb mongodb-query
marked as duplicate by Neil Lunn
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 21 '18 at 19:58
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.
add a comment |
This question already has an answer here:
batchSize field name ignored in Field Projection
2 answers
Again, using mongo v3.4 and referencing these docs: https://docs.mongodb.com/v3.4/tutorial/project-fields-from-query-results/
My example looks like:
const m = this.getCollection(SOME_COLLECTION);
m.find({
'_id': {
$nin: [Ace, Bay],
},
'value.someCategory': {
$type: 'object',
},
}, {
'_id': 0,
'value.someCategory': 1,
}).toArray((err, doc) => {
if (err) {
console.log(err);
} else {
console.log(doc);
}
});
My doc
array will return all items that follow my filter of value.someCategory
of type object, but will not remove the _id
and will return all fields even though I'd like to specify only the value.someCategory
field.
Example data in mongo:
[
{
_id: 'hello',
value: {
someCategory: [Object],
name: 'hello',
otherCategory: true,
}
},
{
_id: 'Ace',
value: {
someCategory: [Object],
name: 'Ace',
otherCategory: true,
}
},
{
_id: 'testing',
value: {
someCategory: null,
name: 'testing',
otherCategory: true,
}
},
]
And expect the result to be:
[
{
someCategory: [Object],
},
]
Based on the docs linked above, the specified fields should be the second param. I'm wondering now if the use of toArray
affects the return value?
node.js mongodb mongodb-query
This question already has an answer here:
batchSize field name ignored in Field Projection
2 answers
Again, using mongo v3.4 and referencing these docs: https://docs.mongodb.com/v3.4/tutorial/project-fields-from-query-results/
My example looks like:
const m = this.getCollection(SOME_COLLECTION);
m.find({
'_id': {
$nin: [Ace, Bay],
},
'value.someCategory': {
$type: 'object',
},
}, {
'_id': 0,
'value.someCategory': 1,
}).toArray((err, doc) => {
if (err) {
console.log(err);
} else {
console.log(doc);
}
});
My doc
array will return all items that follow my filter of value.someCategory
of type object, but will not remove the _id
and will return all fields even though I'd like to specify only the value.someCategory
field.
Example data in mongo:
[
{
_id: 'hello',
value: {
someCategory: [Object],
name: 'hello',
otherCategory: true,
}
},
{
_id: 'Ace',
value: {
someCategory: [Object],
name: 'Ace',
otherCategory: true,
}
},
{
_id: 'testing',
value: {
someCategory: null,
name: 'testing',
otherCategory: true,
}
},
]
And expect the result to be:
[
{
someCategory: [Object],
},
]
Based on the docs linked above, the specified fields should be the second param. I'm wondering now if the use of toArray
affects the return value?
This question already has an answer here:
batchSize field name ignored in Field Projection
2 answers
node.js mongodb mongodb-query
node.js mongodb mongodb-query
edited Nov 21 '18 at 19:21
Anthony Winzlet
14.2k41239
14.2k41239
asked Nov 21 '18 at 18:59
philip yoophilip yoo
1,32331326
1,32331326
marked as duplicate by Neil Lunn
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 21 '18 at 19:58
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
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 21 '18 at 19:58
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.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Use .project
cursor method instead
db.collection('collection')
.find({ '_id': { '$nin': [Ace, Bay] }, 'value.someCategory': { '$type': 'object' }})
.project({ '_id': 0, 'value.someCategory': 1 })
.toArray()
1
great! ty for the quick reply. I tested just now and works :) One slight difference is that the returned value is included within the keyvalue
field like:[{ value: { someCategory: [Object] }}]
. Just wanted to mention this if anyone references this question/answer so they know. Thanks!
– philip yoo
Nov 21 '18 at 19:14
1
If you want tosomeCategory
to top level then you have to use aggregation here. Find query can only filter and limit the data but cannot reshape the output result.
– Anthony Winzlet
Nov 21 '18 at 19:17
Ah great, good to know! Thanks for your help
– philip yoo
Nov 21 '18 at 19:21
add a comment |
use select function and define your required field to projection.
const m = await Model.find({
'_id': {
$nin: [Ace, Bay],
},
'value.someCategory': {
$type: 'object',
},
}).select('-_id value.someCategory').lean();
console.log(m)
Sorry, I'm not using mongoose but thanks for the answer/reply
– philip yoo
Nov 21 '18 at 19:30
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use .project
cursor method instead
db.collection('collection')
.find({ '_id': { '$nin': [Ace, Bay] }, 'value.someCategory': { '$type': 'object' }})
.project({ '_id': 0, 'value.someCategory': 1 })
.toArray()
1
great! ty for the quick reply. I tested just now and works :) One slight difference is that the returned value is included within the keyvalue
field like:[{ value: { someCategory: [Object] }}]
. Just wanted to mention this if anyone references this question/answer so they know. Thanks!
– philip yoo
Nov 21 '18 at 19:14
1
If you want tosomeCategory
to top level then you have to use aggregation here. Find query can only filter and limit the data but cannot reshape the output result.
– Anthony Winzlet
Nov 21 '18 at 19:17
Ah great, good to know! Thanks for your help
– philip yoo
Nov 21 '18 at 19:21
add a comment |
Use .project
cursor method instead
db.collection('collection')
.find({ '_id': { '$nin': [Ace, Bay] }, 'value.someCategory': { '$type': 'object' }})
.project({ '_id': 0, 'value.someCategory': 1 })
.toArray()
1
great! ty for the quick reply. I tested just now and works :) One slight difference is that the returned value is included within the keyvalue
field like:[{ value: { someCategory: [Object] }}]
. Just wanted to mention this if anyone references this question/answer so they know. Thanks!
– philip yoo
Nov 21 '18 at 19:14
1
If you want tosomeCategory
to top level then you have to use aggregation here. Find query can only filter and limit the data but cannot reshape the output result.
– Anthony Winzlet
Nov 21 '18 at 19:17
Ah great, good to know! Thanks for your help
– philip yoo
Nov 21 '18 at 19:21
add a comment |
Use .project
cursor method instead
db.collection('collection')
.find({ '_id': { '$nin': [Ace, Bay] }, 'value.someCategory': { '$type': 'object' }})
.project({ '_id': 0, 'value.someCategory': 1 })
.toArray()
Use .project
cursor method instead
db.collection('collection')
.find({ '_id': { '$nin': [Ace, Bay] }, 'value.someCategory': { '$type': 'object' }})
.project({ '_id': 0, 'value.someCategory': 1 })
.toArray()
answered Nov 21 '18 at 19:08
Anthony WinzletAnthony Winzlet
14.2k41239
14.2k41239
1
great! ty for the quick reply. I tested just now and works :) One slight difference is that the returned value is included within the keyvalue
field like:[{ value: { someCategory: [Object] }}]
. Just wanted to mention this if anyone references this question/answer so they know. Thanks!
– philip yoo
Nov 21 '18 at 19:14
1
If you want tosomeCategory
to top level then you have to use aggregation here. Find query can only filter and limit the data but cannot reshape the output result.
– Anthony Winzlet
Nov 21 '18 at 19:17
Ah great, good to know! Thanks for your help
– philip yoo
Nov 21 '18 at 19:21
add a comment |
1
great! ty for the quick reply. I tested just now and works :) One slight difference is that the returned value is included within the keyvalue
field like:[{ value: { someCategory: [Object] }}]
. Just wanted to mention this if anyone references this question/answer so they know. Thanks!
– philip yoo
Nov 21 '18 at 19:14
1
If you want tosomeCategory
to top level then you have to use aggregation here. Find query can only filter and limit the data but cannot reshape the output result.
– Anthony Winzlet
Nov 21 '18 at 19:17
Ah great, good to know! Thanks for your help
– philip yoo
Nov 21 '18 at 19:21
1
1
great! ty for the quick reply. I tested just now and works :) One slight difference is that the returned value is included within the key
value
field like: [{ value: { someCategory: [Object] }}]
. Just wanted to mention this if anyone references this question/answer so they know. Thanks!– philip yoo
Nov 21 '18 at 19:14
great! ty for the quick reply. I tested just now and works :) One slight difference is that the returned value is included within the key
value
field like: [{ value: { someCategory: [Object] }}]
. Just wanted to mention this if anyone references this question/answer so they know. Thanks!– philip yoo
Nov 21 '18 at 19:14
1
1
If you want to
someCategory
to top level then you have to use aggregation here. Find query can only filter and limit the data but cannot reshape the output result.– Anthony Winzlet
Nov 21 '18 at 19:17
If you want to
someCategory
to top level then you have to use aggregation here. Find query can only filter and limit the data but cannot reshape the output result.– Anthony Winzlet
Nov 21 '18 at 19:17
Ah great, good to know! Thanks for your help
– philip yoo
Nov 21 '18 at 19:21
Ah great, good to know! Thanks for your help
– philip yoo
Nov 21 '18 at 19:21
add a comment |
use select function and define your required field to projection.
const m = await Model.find({
'_id': {
$nin: [Ace, Bay],
},
'value.someCategory': {
$type: 'object',
},
}).select('-_id value.someCategory').lean();
console.log(m)
Sorry, I'm not using mongoose but thanks for the answer/reply
– philip yoo
Nov 21 '18 at 19:30
add a comment |
use select function and define your required field to projection.
const m = await Model.find({
'_id': {
$nin: [Ace, Bay],
},
'value.someCategory': {
$type: 'object',
},
}).select('-_id value.someCategory').lean();
console.log(m)
Sorry, I'm not using mongoose but thanks for the answer/reply
– philip yoo
Nov 21 '18 at 19:30
add a comment |
use select function and define your required field to projection.
const m = await Model.find({
'_id': {
$nin: [Ace, Bay],
},
'value.someCategory': {
$type: 'object',
},
}).select('-_id value.someCategory').lean();
console.log(m)
use select function and define your required field to projection.
const m = await Model.find({
'_id': {
$nin: [Ace, Bay],
},
'value.someCategory': {
$type: 'object',
},
}).select('-_id value.someCategory').lean();
console.log(m)
answered Nov 21 '18 at 19:20
Raunik SinghRaunik Singh
844
844
Sorry, I'm not using mongoose but thanks for the answer/reply
– philip yoo
Nov 21 '18 at 19:30
add a comment |
Sorry, I'm not using mongoose but thanks for the answer/reply
– philip yoo
Nov 21 '18 at 19:30
Sorry, I'm not using mongoose but thanks for the answer/reply
– philip yoo
Nov 21 '18 at 19:30
Sorry, I'm not using mongoose but thanks for the answer/reply
– philip yoo
Nov 21 '18 at 19:30
add a comment |