These are chat archives for Automattic/mongoose

8th
Jul 2018
atoscanoricardo
@atoscanoricardo
Jul 08 2018 03:11 UTC
Hi, how I can obtain ids after insert with insertMany?
Kev
@lineus
Jul 08 2018 09:36 UTC
@atoscanoricardo If you pass {rawResult: true} in the options, the result object returned in the callback has a field called insertedIds that contains them. Otherwise the default return from mongoose is an array of the docs that passed validation.
cosminn777
@cosminn777
Jul 08 2018 09:48 UTC
@lineus thank you for your answer. If I try passing an object I get this (when wanting to remove one field): MongoError: Unsupported projection option: nestedObject: { field: 0 }, and I get this when wanting to remove 2 fields from the nested object: MongoError: >1 field in obj: { fieldA: 0, fieldB: 0 }
Kev
@lineus
Jul 08 2018 09:50 UTC
@cosminn777 I tested the projection with a string and it works for me in this gist. What version of mongoose are you running?
cosminn777
@cosminn777
Jul 08 2018 10:07 UTC
@lineus I'm running MongoDB 4.0 and Mongoose 5.2.1
Kev
@lineus
Jul 08 2018 10:14 UTC
@cosminn777 I re-ran my example against mongodb 4 and I get the same result that I got with 3.6.5. Can you create an example ( or fork mine ) and show what you're doing differently from me?
cosminn777
@cosminn777
Jul 08 2018 10:15 UTC
yes, I ran the example too and it works for me too
I'll try to dig deeper
cosminn777
@cosminn777
Jul 08 2018 10:31 UTC
I currently can't replicate the issue on the test code, I'll work on it and come back when I have an answer, thank you @lineus so much!
Kev
@lineus
Jul 08 2018 10:32 UTC
anytime :smile:
cosminn777
@cosminn777
Jul 08 2018 12:17 UTC
@lineus I posted a test code where the exclusion is not working, I used 2 collections, one for main object and one for the nested object
Kev
@lineus
Jul 08 2018 12:26 UTC
@cosminn777 when you ran that did you have the port specified in the uri string? I don't think it will run at all without it on 5.2 at the moment
I'll take a look and give it a try now
cosminn777
@cosminn777
Jul 08 2018 12:28 UTC
@lineus yes, I had the user:pass and port specified in the uri string, I just removed those uri details in the example
thank you
mongodb://user:pass@127.0.0.1:port/test
Kev
@lineus
Jul 08 2018 12:30 UTC
right on, that makes sense
cosminn777
@cosminn777
Jul 08 2018 12:30 UTC
updated the comment on git too
Kev
@lineus
Jul 08 2018 12:30 UTC
it explains why you have the authSource option on your connection too :)
in your example, the select() applies to the findOne, not the population.
cosminn777
@cosminn777
Jul 08 2018 12:33 UTC
well, in my actual code I have find() instead of findOne(), but I guess the issue is the same
but moving select() before populate() yelds the same result
am I doing something wrong?
Kev
@lineus
Jul 08 2018 12:36 UTC
you need to pass an object defining your population with a select there. I'm in the process of updating it now.
  let pop = { 
    path: 'testNested',
    select: '-tags -tagsb -_id'
  };
  Test.findOne({})
    .sort({})
    .limit(100)
    .populate(pop)
    .lean(true)
    .exec(function (err, res) {
      console.log(err, res);
    });
results:
gitter: ./cosminn777_2.js
null { _id: 5b4205585ad5ea211e9fd4a1,
  testNested:
   { name: 'Nested Name',
     createdAt: 2018-07-08T12:36:40.435Z,
     updatedAt: 2018-07-08T12:36:40.435Z,
     __v: 0 },
  createdAt: 2018-07-08T12:36:40.112Z,
  updatedAt: 2018-07-08T12:36:40.112Z,
  __v: 0 }
^C
gitter:
cosminn777
@cosminn777
Jul 08 2018 12:39 UTC
I see
it's working now
but why isn't the select('-testNested.tags' ) not working? shouldn't it work?
Kev
@lineus
Jul 08 2018 12:43 UTC
I believe there's an open bug for the excluded field showing up in the results after population actually. Not sure if the fix for it will be in the next release 5.2.2 or if it will be in 5.2.3
cosminn777
@cosminn777
Jul 08 2018 12:45 UTC
alright, thank you @lineus!
Kev
@lineus
Jul 08 2018 12:48 UTC
oh wait, it wasn't the confirmed bug I was thinking of, I was thinking of an ongoing discussion that's only tangentially related to this. TL:DR populate can override select(), if you need only certain fields from a populate call, use the populate select field.
sorry :)
you can also pass it like .populate('testNested', '-tags -tagsb -_id')
cosminn777
@cosminn777
Jul 08 2018 12:50 UTC
ok, so this is a bug, then I will create an Issue on github for this
Kev
@lineus
Jul 08 2018 12:54 UTC
checkout Automattic/mongoose#6546 before you open a new one, I think it's related to your usecase
cosminn777
@cosminn777
Jul 08 2018 12:58 UTC
it seems related but it's not, the user wants to include only what he specified in select(), I want to exclude fields excluded in select()
Kev
@lineus
Jul 08 2018 13:13 UTC
your example in the issue would be more clear without the queries B and C, as those errors are from mongodb, those projections are invalid.
cosminn777
@cosminn777
Jul 08 2018 13:18 UTC
you are right
I created an issue here: Automattic/mongoose#6696
Ian Paschal
@ianpaschal
Jul 08 2018 22:19 UTC
Hello, anyone have any idea why the "extended" portions of my schema are not saved to the database when using discriminators?
I have set discriminatorKey on both models' options object
everything "works fine", no errors or anything, but the special fields in the extended version of the model is simply ignored.
Kev
@lineus
Jul 08 2018 23:38 UTC
@ianpaschal can you share a gist of your schematic declarations for your model and discriminators?