These are chat archives for Automattic/mongoose

17th
Apr 2015
Ulf Seltmann
@smoebody
Apr 17 2015 08:57
hi all. i have a problem with configuring mongoose plugins. the config values are not available at plugin initialisation which is done when i require the model. is there any way of changing the settings afterwards or what is the best practise here?
Stephan Kristyn
@nottinhill
Apr 17 2015 10:16
hi there I have a not so trivial problem, getting: "exceeded maximum write batch size of 1000" when using bulk insert and a .toObject before hand, and yes my document is huge. how to workaround this *"??!%&&%&!" MongoDB limitation?
Erinç Fırtına
@EricMcRay
Apr 17 2015 12:55
Hi people! I want to ask a small question which could not find any answer anywhere. I am trying to use a mongoose model in another model's static. is that possible?
For exmaple:
B.statics.doIt = function(id,cb){
A.findOneAndUpdate({_id: id}, { $set: { yes: true }}, cb)
}
Stephan Kristyn
@nottinhill
Apr 17 2015 12:58
x-posted my issue from above for further clarification: Automattic/mongoose#2893
uhm I think I fuond the error..
Stephan Kristyn
@nottinhill
Apr 17 2015 13:04
no did not lol
Maksim
@chetverikov
Apr 17 2015 13:07
:point_up: 17 апреля 2015 г., 20:55
Why not?
  B.statics.wtf = function( id, cb ){
    mongoose.model(‘A’).findOneAndUpdate({_id: id}, { $set: { yes: true }}, cb);
  }
Erinç Fırtına
@EricMcRay
Apr 17 2015 13:09
@chetverikov I require A from another file which exports mongoose.model('A', ASchema)
Erinç Fırtına
@EricMcRay
Apr 17 2015 13:16
it work if I call it like you said, wierd...
Valeri Karpov
@vkarpov15
Apr 17 2015 13:27
@chetverikov 's way works. Not the way I would write it but it gets the job done
Maksim
@chetverikov
Apr 17 2015 13:27
opa
oops
@vkarpov15 when can we expect the release of 4.0.2?
And... I got an idea for the great features!
Valeri Karpov
@vkarpov15
Apr 17 2015 13:30
milestones say April 24, I'll probably do a couple days before that though, since there's a lot of bug fixes :)
Maksim
@chetverikov
Apr 17 2015 13:31
Can I do the join method. It will allow you to retrieve documents and to mix them in anything by using additional queries using join. How about this idea?
Valeri Karpov
@vkarpov15
Apr 17 2015 13:34
Sounds like an interesting idea, but please for the love of god don't call it join() :)
don't want people thinking that it's exactly the same as SQL join with all of its associated quirks
Erinç Fırtına
@EricMcRay
Apr 17 2015 14:41
I use same populate queries at different controller so I want to group them. How can I do that? I tried that to do it with static but did not work
For example:
A.find().specialPopulate().exec()
special populate is equal to query.populate('a').populate('b').populate('c')
Maksim
@chetverikov
Apr 17 2015 15:17
I do not understand your problem... :(
Erinç Fırtına
@EricMcRay
Apr 17 2015 15:19
sorry :D
let me give an example
my current code: A.find().populate('aField').populate('bField').populate('cField').exec()
I use these populate in others controllers too and I dont want to repeat that everywhere
Maksim
@chetverikov
Apr 17 2015 15:21
You can populate all three fields: A.find().populate(‘aField bField cField’).exec()
or object with parameters
Erinç Fırtına
@EricMcRay
Apr 17 2015 15:21
so I want to merge that 3 populate query to single one
and I also get only few fields from populated docs
like populate('aField', 'name lastname mobile').populate('bField', 'field1 field3 field5')
Maksim
@chetverikov
Apr 17 2015 15:24
ok:

A.find().populate([
  {
    path: "aField",
    select: "some fields"
  },

  {
    path: "bField",
    select: "some fields"
  },

  {
    path: "cField",
    select: "some fields",
    match: {} // filters
  }
]).exec()
Erinç Fırtına
@EricMcRay
Apr 17 2015 15:25
oh ok I can use array for populate
nice
Maksim
@chetverikov
Apr 17 2015 15:27
:)
Erinç Fırtına
@EricMcRay
Apr 17 2015 15:27
can i separate that populate query to another place. I want to keep my controllers looks clean and readable
Maksim
@chetverikov
Apr 17 2015 15:27
???
Erinç Fırtına
@EricMcRay
Apr 17 2015 15:27
I am trying to do it like modelA.find().summary().exec()
Maksim
@chetverikov
Apr 17 2015 15:28
oh...
so...
I think it is better to implement in static methods
modelA.findWithMegaPopulate
Erinç Fırtına
@EricMcRay
Apr 17 2015 15:29
but If I do it in static method I cant use it like modalA.find().summary() and also modalA.findOne().summary().exec()
this will be very useful
i think this feature can be in with mongoose 4.0 :D
Maksim
@chetverikov
Apr 17 2015 15:30
:)
you can write proxy method….
  schemaA.statics.summary = function(name, args, cb){
    this[name].apply(this, args).populate( {•••} ).exec(cb);
  }
something like this
And use modelA.summary(‘findOne’, [ ARGS ], function(){})
Erinç Fırtına
@EricMcRay
Apr 17 2015 15:35
ok got it
thanks for help
I going to try it
Maksim
@chetverikov
Apr 17 2015 15:36
:+1:
I think this should help. But it is better to get the data using aggregate, and then, if necessary, to fill the model....
It will be faster, but one collection +(
сonvenient in analytics
feargal9
@feargal9
Apr 17 2015 17:37
Hi guys, quick question. I have this expression 'console.log(Tickit.find({type: 'customId'}).count() + 1);' which prints out '[object Object]1'. Is there a way that I can just get the number value so it can be assigned to a different property, and not have the [object Object] ?
tushar dhingra
@tushki
Apr 17 2015 17:44
hi all
i have installed nodejs and now trying to install express mongoosastic via same
using npm install
Capture.PNG
but i getting the above image please help me to understand it
how could i solve this problem
Valeri Karpov
@vkarpov15
Apr 17 2015 19:00
don't run npm install in the node repl @tushki
@feargal9 that's not how things work in nodejs, Tickit.find({type: 'customId'}).count() returns a promise rather than a number. Do Tickit.find({type: 'customId'}).count(function(err, count) { console.log(count + 1); }) ; instead
tushar dhingra
@tushki
Apr 17 2015 19:01
its not in the node @vkarpov15 when i did by mistake it displayed to use ctrl +d
i did and running it then
tushar dhingra
@tushki
Apr 17 2015 19:12
@vkarpov15 are you der ?
could you please help me with it ?
Valeri Karpov
@vkarpov15
Apr 17 2015 21:12
that's the only error I see in the image, the results of npm install express are cut off
Sean Quinn
@swquinn
Apr 17 2015 21:49

If I'm writing tests against a query that leverages promises (e.g. findOne({ username: 'johndoe' }).exec()) what would be the right way to go about testing that method in Mocha and Chai? I have something similar to the following:

describe('test', function() {
  it('should return the user matching the username', function() {
    var promise = User.findOne({ username: 'johndoe' }).exec();
    promise.then(function(result) {
      // assertions
    });
  });
});

I'm finding that if my assertions fail within the promise I'm not seeing those failures and it looks like the test is passing. Any advice?