Speeding up MongoBD query { $ne: [] }
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I'm currently doing a MongoDB aggregation but then have a query looking for all documents where a certain field, whose value is an array, is empty.
obs = db.collection.count({'things.titles': {'$ne': }})
To ensure this query uses an index, do I just need to do something like this?:
db.collection.ensureIndex({'things.titles': 1})
I understand this creates a multikey index, but it still takes a very long time (more than an hour) on a collection sized at 4739208
documents.
mongodb
bumped to the homepage by Community♦ 11 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I'm currently doing a MongoDB aggregation but then have a query looking for all documents where a certain field, whose value is an array, is empty.
obs = db.collection.count({'things.titles': {'$ne': }})
To ensure this query uses an index, do I just need to do something like this?:
db.collection.ensureIndex({'things.titles': 1})
I understand this creates a multikey index, but it still takes a very long time (more than an hour) on a collection sized at 4739208
documents.
mongodb
bumped to the homepage by Community♦ 11 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
How long is it taking for db.collection.count({'things.titles': }). Also read docs.mongodb.org/manual/core/query-optimization/…
– titogeo
Apr 20 '16 at 12:04
Oddly, a brief few seconds @titogeo.
– dalanmiller
Apr 20 '16 at 14:51
In my tests using size is faster by 3 times than using ne - db.collection.count({'things.titles': { $size : 0}}); Is (db.collection.count() (minus) db.collection.count({'things.titles': { $size : 0}})) faster than using ne. My data size is small.
– titogeo
Apr 21 '16 at 5:17
add a comment |
I'm currently doing a MongoDB aggregation but then have a query looking for all documents where a certain field, whose value is an array, is empty.
obs = db.collection.count({'things.titles': {'$ne': }})
To ensure this query uses an index, do I just need to do something like this?:
db.collection.ensureIndex({'things.titles': 1})
I understand this creates a multikey index, but it still takes a very long time (more than an hour) on a collection sized at 4739208
documents.
mongodb
I'm currently doing a MongoDB aggregation but then have a query looking for all documents where a certain field, whose value is an array, is empty.
obs = db.collection.count({'things.titles': {'$ne': }})
To ensure this query uses an index, do I just need to do something like this?:
db.collection.ensureIndex({'things.titles': 1})
I understand this creates a multikey index, but it still takes a very long time (more than an hour) on a collection sized at 4739208
documents.
mongodb
mongodb
asked Apr 19 '16 at 16:04
dalanmillerdalanmiller
1164
1164
bumped to the homepage by Community♦ 11 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 11 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
How long is it taking for db.collection.count({'things.titles': }). Also read docs.mongodb.org/manual/core/query-optimization/…
– titogeo
Apr 20 '16 at 12:04
Oddly, a brief few seconds @titogeo.
– dalanmiller
Apr 20 '16 at 14:51
In my tests using size is faster by 3 times than using ne - db.collection.count({'things.titles': { $size : 0}}); Is (db.collection.count() (minus) db.collection.count({'things.titles': { $size : 0}})) faster than using ne. My data size is small.
– titogeo
Apr 21 '16 at 5:17
add a comment |
How long is it taking for db.collection.count({'things.titles': }). Also read docs.mongodb.org/manual/core/query-optimization/…
– titogeo
Apr 20 '16 at 12:04
Oddly, a brief few seconds @titogeo.
– dalanmiller
Apr 20 '16 at 14:51
In my tests using size is faster by 3 times than using ne - db.collection.count({'things.titles': { $size : 0}}); Is (db.collection.count() (minus) db.collection.count({'things.titles': { $size : 0}})) faster than using ne. My data size is small.
– titogeo
Apr 21 '16 at 5:17
How long is it taking for db.collection.count({'things.titles': }). Also read docs.mongodb.org/manual/core/query-optimization/…
– titogeo
Apr 20 '16 at 12:04
How long is it taking for db.collection.count({'things.titles': }). Also read docs.mongodb.org/manual/core/query-optimization/…
– titogeo
Apr 20 '16 at 12:04
Oddly, a brief few seconds @titogeo.
– dalanmiller
Apr 20 '16 at 14:51
Oddly, a brief few seconds @titogeo.
– dalanmiller
Apr 20 '16 at 14:51
In my tests using size is faster by 3 times than using ne - db.collection.count({'things.titles': { $size : 0}}); Is (db.collection.count() (minus) db.collection.count({'things.titles': { $size : 0}})) faster than using ne. My data size is small.
– titogeo
Apr 21 '16 at 5:17
In my tests using size is faster by 3 times than using ne - db.collection.count({'things.titles': { $size : 0}}); Is (db.collection.count() (minus) db.collection.count({'things.titles': { $size : 0}})) faster than using ne. My data size is small.
– titogeo
Apr 21 '16 at 5:17
add a comment |
1 Answer
1
active
oldest
votes
Your question specifies:
... a query looking for all documents where a certain field, whose value is an array, is empty.
Your query is count all documents where the array is not empty, is this intended?
Depending upon the cardinality of the values of this field, this could be causing these values to be expensive.
To query for empty arrays, the operation would be:
db.collection.explain().count( 'things.titles' : [ ] } )
Note that there are edge cases to this query. For example, the following would be returned:
{ "_id" : ..., "things.titles" : [ [ ] ] }
The index usage of the count operation can be determined using the collection.explain() function.
For example:
db.collection.explain().count({'things.titles': {'$ne': }})
The output will provide information showing the index bounds being used (if they exist).
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2fdba.stackexchange.com%2fquestions%2f135800%2fspeeding-up-mongobd-query-ne%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your question specifies:
... a query looking for all documents where a certain field, whose value is an array, is empty.
Your query is count all documents where the array is not empty, is this intended?
Depending upon the cardinality of the values of this field, this could be causing these values to be expensive.
To query for empty arrays, the operation would be:
db.collection.explain().count( 'things.titles' : [ ] } )
Note that there are edge cases to this query. For example, the following would be returned:
{ "_id" : ..., "things.titles" : [ [ ] ] }
The index usage of the count operation can be determined using the collection.explain() function.
For example:
db.collection.explain().count({'things.titles': {'$ne': }})
The output will provide information showing the index bounds being used (if they exist).
add a comment |
Your question specifies:
... a query looking for all documents where a certain field, whose value is an array, is empty.
Your query is count all documents where the array is not empty, is this intended?
Depending upon the cardinality of the values of this field, this could be causing these values to be expensive.
To query for empty arrays, the operation would be:
db.collection.explain().count( 'things.titles' : [ ] } )
Note that there are edge cases to this query. For example, the following would be returned:
{ "_id" : ..., "things.titles" : [ [ ] ] }
The index usage of the count operation can be determined using the collection.explain() function.
For example:
db.collection.explain().count({'things.titles': {'$ne': }})
The output will provide information showing the index bounds being used (if they exist).
add a comment |
Your question specifies:
... a query looking for all documents where a certain field, whose value is an array, is empty.
Your query is count all documents where the array is not empty, is this intended?
Depending upon the cardinality of the values of this field, this could be causing these values to be expensive.
To query for empty arrays, the operation would be:
db.collection.explain().count( 'things.titles' : [ ] } )
Note that there are edge cases to this query. For example, the following would be returned:
{ "_id" : ..., "things.titles" : [ [ ] ] }
The index usage of the count operation can be determined using the collection.explain() function.
For example:
db.collection.explain().count({'things.titles': {'$ne': }})
The output will provide information showing the index bounds being used (if they exist).
Your question specifies:
... a query looking for all documents where a certain field, whose value is an array, is empty.
Your query is count all documents where the array is not empty, is this intended?
Depending upon the cardinality of the values of this field, this could be causing these values to be expensive.
To query for empty arrays, the operation would be:
db.collection.explain().count( 'things.titles' : [ ] } )
Note that there are edge cases to this query. For example, the following would be returned:
{ "_id" : ..., "things.titles" : [ [ ] ] }
The index usage of the count operation can be determined using the collection.explain() function.
For example:
db.collection.explain().count({'things.titles': {'$ne': }})
The output will provide information showing the index bounds being used (if they exist).
answered Oct 22 '16 at 0:14
Adam HarrisonAdam Harrison
24815
24815
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- 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%2fdba.stackexchange.com%2fquestions%2f135800%2fspeeding-up-mongobd-query-ne%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
How long is it taking for db.collection.count({'things.titles': }). Also read docs.mongodb.org/manual/core/query-optimization/…
– titogeo
Apr 20 '16 at 12:04
Oddly, a brief few seconds @titogeo.
– dalanmiller
Apr 20 '16 at 14:51
In my tests using size is faster by 3 times than using ne - db.collection.count({'things.titles': { $size : 0}}); Is (db.collection.count() (minus) db.collection.count({'things.titles': { $size : 0}})) faster than using ne. My data size is small.
– titogeo
Apr 21 '16 at 5:17