thoughtAddict
unread,Aug 3, 2011, 10:08:35 PM8/3/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Mongoose Node.JS ORM
Hello. Wondering if someone could point me in the right direction. I
have a feeling it's something simple that I'm missing....
I'm seeing that when I run a "find" on one of my collections, the
MongoDB Shell response is in milliseconds, while the Mongoose response
is in seconds.
I'm wondering if it's:
------------------------------------------
1. Due to my creation of the objects initially without an "index"
1a. I later indexed the same collection on the fields in the "find"
2. Something I'm doing incorrectly within the Mongoose "find" command
Background
============================================
** Schema
**------------------------------------------
var SessionSchema = new Schema({
customerId : { type: String }
, startTimeMS : { type: Number }
, ...
, ...
, ...
});
** Same Schema (Changed later to indexing)
**------------------------------------------
var SessionSchema = new Schema({
customerId : { type: String, index: true }
, startTimeMS : { type: Number, index: true }
, ...
, ...
, ...
});
** Indexes (Added at same time with the Schema indexing above, but not
initially used)
**------------------------------------------
SessionSchema.index({ "customerId": 1 });
SessionSchema.index({ "startTimeMS": 1 });
** I also tried a Compound index, which didn't appear to make a
difference
**------------------------------------------
SessionSchema.index({ "customerId": 1, "startTimeMS": 1 });
** Init the model
**------------------------------------------
mongoose.model('Session', SessionSchema)
var Session = mongoose.model('Session');
** Then I have a script that randomly generates some test Sessions for
me
**------------------------------------------
var session = new Session();
session.startTime = (new Date()).setTime(startDateMS);
session.startTimeMS = startDateMS;
session.customerId = customerId;
session.etc
session.etc
session.etc
session.etc
session.save(function (err) {
if (!err) {
console.log("SESSION Creationg: (" + session.startTime +
")");
} else {
console.log(err);
}
});
** I have 60,000 of these Session objects in a "sessions" collection
** When executing in the MongoDB Shell:
db.sessions.find( { "customerId":"4e2fcc15a05e8e7f57000001",
"startTimeMS":{ "$gte":1312135543649, "$lte":1312394743649 } } )
** I get
{
"cursor" : "BtreeCursor startTimeMS_1",
"nscanned" : 6867,
"nscannedObjects" : 6867,
"n" : 6867,
"millis" : 60,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
"startTimeMS" : [
[
1312135543649,
1312394743649
]
]
}
}
** 60 milliseconds is fine with me, but when I try the same with a
Mongoose "find"
Session.find(
{
"customerId":cid,
"startTimeMS":{
"$gte":startTimeMS,
"$lte":endTimeMS
}
}, callback);
** or
Session.where("customerId",
cid).where("startTimeMS").gte(startTimeMS).lte(endTimeMS).find(callback)
** it takes about 9 to 11 seconds.
I've reindexed my collection on:
customerId
startTimeMS
and both (compound)
Any ideas?
Thanks,
Jon