These are chat archives for Automattic/mongoose

29th
Jan 2016
sandy240287
@sandy240287
Jan 29 2016 08:16
Hey I am trying to run a query in mongoose .. The query is - { $and : [{ $or: [ {date: /2016-01/ },{date: /2015-12/ },{date: /2015-11/ } ]},{ $or: [ {symbol: 'GOOG'},{symbol: 'AAPL'}]}]} The same query runs fine in Mongo DB directly .. Please suggest the possible reasons..
Konstantin Baierer
@kba
Jan 29 2016 10:07
@sandy240287 Do the date-per-regex queries work on their own, outside the '$or' / '$and' clauses?
If you want to search with regexes, date must be a string field. Wouldn't it be better to define them as native Date objects and query them as such?
Valeri Karpov
@vkarpov15
Jan 29 2016 15:35
also @sandy240287 what does your schema look like? Are the date objects defined as type 'Date' in the schema, or 'String'?
Andrei Demian
@unDemian
Jan 29 2016 16:08
hey, i'm working on doing a basic bulk insert for hours now :) the code is something like AppInfo.collection.insert(entry, function(error, docs) { context.logger.info(error); context.logger.info(error)}); and now I get the error {"code":17280,"index":0,"errmsg":"WiredTigerIndex::insert: key too large to index, failing 2296
any ideas? thanks
the collections is empty
with index only for _id
Konstantin Baierer
@kba
Jan 29 2016 16:15
@unDemian The index key must not exceed 1024 bytes by default, see https://docs.mongodb.org/manual/reference/limits/#indexes
Why is your _id field so large?
Andrei Demian
@unDemian
Jan 29 2016 16:16
I don't have an _id field
my object is something like
var data = {
  created_ts: new Date(),
  hash: payload,
  data: data
};
where payload and data are big objects
should I manually create and _id: mongoose.Type.ObjectId() ?
Konstantin Baierer
@kba
Jan 29 2016 16:23
If you rely on the default _id index and use ObjectId() as _id, I wouldn't know how this could happen. Does the error happen in the insert function you showed or on index? If it happens on insert, what is the last document you're inserting? Are you sure that none of the documents have a _id field that might cause this?
Andrei Demian
@unDemian
Jan 29 2016 16:28
insert
I dont have any index
hmm I'll double check
there was an _id nested
lol
I thought is looking only on first level
I'm kinda new to mongo, but man this is crap. even if I provied a first level _id it still uses the one that is 5 levels down the json object. is there a way to bypass this?
Konstantin Baierer
@kba
Jan 29 2016 16:33
You mean, like entry.has._id or entry.data._id? No, it should not be indexed, this is weird behavior.
Andrei Demian
@unDemian
Jan 29 2016 16:34
entry.data.whatever._id
Konstantin Baierer
@kba
Jan 29 2016 16:34
What's your schema for entry?
Andrei Demian
@unDemian
Jan 29 2016 16:34
and I added entry._id but is ignored
the one that I posted above
but in data at some level there is on _id
var data = {
            _id: mongoose.Types.ObjectId(),
            created_ts: new Date(),
            hash: payload,
            data: data
        };
Konstantin Baierer
@kba
Jan 29 2016 16:35
Yeah, but the Mongoose.Schema that defines entry?
Andrei Demian
@unDemian
Jan 29 2016 16:35
ah
it has no _id
but I thought that if I call Model.collection.insert it bypasses entirely my schema
Konstantin Baierer
@kba
Jan 29 2016 16:37
ah right.
Andrei Demian
@unDemian
Jan 29 2016 16:43
lol, i'm damn stupid
look at the object data { data: data } :))
thanks anyways :)
Konstantin Baierer
@kba
Jan 29 2016 16:48
Ah, I thought that was just example code :) Circular references bad bad bad :D
J
@jhyland87
Jan 29 2016 19:23
Is there a way to check if an object that will be inserted is compliant with the models schema, without inserting it? (Or even creating a new object with the data) I was doing this validation inside a static method of the model, so I have access t the schema
Chris Paradeise
@cparadeise
Jan 29 2016 19:30
pre-hooks?
J
@jhyland87
Jan 29 2016 21:29
@cparadeise Does the pre('save') hook have the data? The examples in there just explain the next and done
Chris Paradeise
@cparadeise
Jan 29 2016 21:30
At the point of the pre('save'), your req object will have whatever data was sent from the client/requester
J
@jhyland87
Jan 29 2016 21:31
Sorry.. Not sure I understand what you mean :-\ (Somewhat new to Mongoose/MDB)
Chris Paradeise
@cparadeise
Jan 29 2016 21:32
I think the hook would activate whenever you execute a 'save' action. It will return an error to the user if the validations in the pre(save) fail, otherwise it will commit the data to the database.
J
@jhyland87
Jan 29 2016 21:32
Right, but how can I get the data being saved from within the pre-save? It doesnt look like its passed as a parameter
Chris Paradeise
@cparadeise
Jan 29 2016 21:32
I'm thinking in the context of sending an Http request to a server hosting a mongo db
I see, there is no req object in the pre event. Trying to remember, is it in the 'this' context inside pre/post hooks?
so you would have a model, {id:'',name:''}. you would be able to get at your data object from 'this.id' and 'this.name'. I think.
J
@jhyland87
Jan 29 2016 21:37
Ohh, I see, hmmm, ill try it out
Chris Paradeise
@cparadeise
Jan 29 2016 21:37
good luck!
J
@jhyland87
Jan 29 2016 21:39
Ah, yep, that works :) Thanks @cparadeise
Chris Paradeise
@cparadeise
Jan 29 2016 21:42
woohoo! happy coding