These are chat archives for Automattic/mongoose

15th
Mar 2018
steadysupply
@steadysupply
Mar 15 2018 12:40
is it possible to add methods to a schema via plugin?
in my plugin code i have written schema.methods.myMethod = function () {} but the model instance doesn't have the method
also model.schema.methods is {}
steadysupply
@steadysupply
Mar 15 2018 14:14
@maintainers maybe add to the docs to mention this limitation?
Kev
@lineus
Mar 15 2018 14:21
hey @steadysupply. I was able to make it work, gist here
hillct
@hillct
Mar 15 2018 16:47
Good afternoon all. I’m trying to assemble a query, whereby I have a set of documents, where each document has an array of ‘tags’. I want to return matching array values, as a single array, across all documents. Any guidence? Is it better to assemble my target single array from returned documents, or can that all be done within the mongo query itself?
Mihajlo Ilijić
@Pritilender
Mar 15 2018 16:49
@hillct you want to get all tags from the db and then do the filtering, right?
@hillct if I get it right, you need distinct
MySchema.distinct('tags', { tags: 'my-tag' })
hillct
@hillct
Mar 15 2018 16:56
@Pritilender yes, but my question was more about taking the filtered results from each of the arrays within all documents, unify the arrays, then run a distinct operation, where I don’t see a reasonable way to perform the unification of result arrays within the query. I assume I’m missing something
@Pritilender or are you suggesting that I do this as individual steps?
Mihajlo Ilijić
@Pritilender
Mar 15 2018 16:58
Let's just see if we're on the same page: if you have document #1 with tags blue, red, green and document #2 with tags yellow, blue, black and document #3 with tags white, red, the result should be blue, red, green, yellow, black, white, right?
hillct
@hillct
Mar 15 2018 16:58
yes
Mihajlo Ilijić
@Pritilender
Mar 15 2018 16:58
If so, I'd suggest taking a look at Model.distinct(the link I've provided)
It does exactly that, where the second param is the filtering option.
hillct
@hillct
Mar 15 2018 16:59
reviewed the link. The docs were ands are not entirely elucidating. your example MySchema.distinct('tags', { tags: 'my-tag' }) makes sense though. I’ll give that a shot
thanks
Mihajlo Ilijić
@Pritilender
Mar 15 2018 16:59
Actaully, I've never used the second param. Not sure if it will run distinct only on documents that have that tag.
or the tag matching that query
But whatever the case is, you actually want that tag, and I see no problem if it filters the documents.
hillct
@hillct
Mar 15 2018 17:01
my impression was that it would return arrays where all the elements would be tested and the ‘distinct’ operation would be performed across each whole array, but will give it a shot. Thanks
@Pritilender my overall goal here is to be able to exteact data to populate an autocompletion UI element
Mihajlo Ilijić
@Pritilender
Mar 15 2018 17:03
Hm. Why don't you hold tags as separate collection than and have refs in your documents to the tags?
It miiiight be faster, not actually sure.
I have to go now. I'll be online in a few hours.
hillct
@hillct
Mar 15 2018 17:03
@Pritilender when initially arctitecting these things that always seems like overkill, then of course you run into things like this
thanks for the help
hillct
@hillct
Mar 15 2018 17:10
@Pritilender It looks like $elemMatch https://docs.mongodb.com/manual/reference/operator/query/elemMatch/ gets us closer, in combination with distinct, though it’s not clear whether I can return only the matching elements
Mihajlo Ilijić
@Pritilender
Mar 15 2018 22:10
@hillct I'm playing around with distinct and this, and all I can see is that .distinct('tags', {tags: 'some-tag'}) returns distinct tags on every document where the query is true
for instance, I have some documents that have color names for tags
this is the query: db.sandbox.distinct('tag', {tag: {$regex: /^g/i}})
these are the results: green, grey, red, white-yellow
As you can see, only green and grey match the regex. The other are tags that are attached to the same document where green and grey are.
So, what would I suggest is maybe to return only distinct tags and then do the filtering based on the entered query.
Mihajlo Ilijić
@Pritilender
Mar 15 2018 22:21
You can, of course, achieve it with aggregation:
const typedRegex = new Regexp(`^${stringToMatch}`, i)
const aggregationResult = await MyModel.aggregate([
    {
      $unwind: {
          path: '$tag',
      } , 
    },
    {
        $match: {
            tag: {$regex: typedRegex},
        },
    },
    {
        $group: {
            _id: null,
            tags: {$addToSet: '$tag'},
        },       
    },
])
const typeaheadMatchd = aggregationResult.tags
Angel Daniel Munoz Gonzalez
@AngelMunoz
Mar 15 2018 23:53

hey guys I have quite a silly question
I want to save a new Document lets say Company with a field called industries which itself it's an array of Industry collection which is unique by name
somewhat like this

const industry = Industry.create({ name: 'it' });
const company = new Company({ name: 'some company' })
company.industries.push(industry)
company.save() // <--- throws a E11000

also when doing something like this happens again

const location = Location.create({ name: 'it' });
const company = new Company({ name: 'some company' })
company.location = location;
company.save() // <--- throws a E11000

I omited the await keywords for brevity but I'm 100% I'm awaiting all saves
how do you save those kinds of attributes?