These are chat archives for Automattic/mongoose

6th
Oct 2016
Bikash Sharma
@bikashsharmabks
Oct 06 2016 06:21

is there any chance upsert query may insert multiple documents instead of updating if it exists, I see this in production enviroment

Here is the query

 model.findOneAndUpdate(criteria, data, {
            'runValidators': true,
            'upsert': true,
            'new': true
        }, function(err, updateResult) {
Jenna Vuong
@jennabelle
Oct 06 2016 08:34

Hello everyone! I am trying to write a query using Mongoose.js but am having difficulty doing so. Does anyone have any tips on how to write this in Mongoose?

SELECT * FROM Account WHERE data.name LIKE Account.Name OR data.name IN Account.Corporate_Names

I feel like I've tried everything already
Thank you in advance!!
Paul "Joey" Clark
@joeytwiddle
Oct 06 2016 08:39

Maybe something like this:

{
  $or: [
    {
      'data.name': new RegExp( escapeRegexp(account.name) )
    },
    {
      'data.name': {$in: ['corporate_name1', 'corporate_name2', '...']}
    }
  ]
}

Unfortuntely you'll have to write/find escapeRegexp() yourself.

Jenna Vuong
@jennabelle
Oct 06 2016 08:39
Interesting
Paul "Joey" Clark
@joeytwiddle
Oct 06 2016 08:41
Oh hmmm. Sorry I thought data.name was in the document, and account.name was in your scope. I think that's not what you wanted. :P
Jenna Vuong
@jennabelle
Oct 06 2016 08:41
Oh thats correct- account.name is in document, data.name is my scope :)
Paul "Joey" Clark
@joeytwiddle
Oct 06 2016 08:43
Account.find({
  $or: [
    {
      name: new RegExp( escapeRegexp(data.name) )
    },
    {
      corporateNames: data.name
    }
  ]
})
Jenna Vuong
@jennabelle
Oct 06 2016 08:43
How would I use $or?
in the query ?
Paul "Joey" Clark
@joeytwiddle
Oct 06 2016 08:44
Yeah that {$or: ...} is the query
Jonathan
@MaddoxDevelopment
Oct 06 2016 08:44
account.find({...})
thats where you would put the $or
Paul "Joey" Clark
@joeytwiddle
Oct 06 2016 08:44
Yes
Jenna Vuong
@jennabelle
Oct 06 2016 08:45
Account.find({ $or }) ?
Paul "Joey" Clark
@joeytwiddle
Oct 06 2016 08:45
Yes. Account.find({ $or: ... })
Jenna Vuong
@jennabelle
Oct 06 2016 08:45
Oh....
Jonathan
@MaddoxDevelopment
Oct 06 2016 08:45
His code snippet is the javascript object of the query
Paul "Joey" Clark
@joeytwiddle
Oct 06 2016 08:45
So {corporateNames: data.name} would usually be an exact match on the field.
But apparently when corporateNames is an Array in the document, then that in fact performs a search through the array, and succeeds if there is a match found.
Jonathan
@MaddoxDevelopment
Oct 06 2016 08:45
Account.find(JAVASCRIPT QUERY HERE)
Jenna Vuong
@jennabelle
Oct 06 2016 08:46
Oh, got it
Paul "Joey" Clark
@joeytwiddle
Oct 06 2016 08:46
I've edited the original now ;)
Jenna Vuong
@jennabelle
Oct 06 2016 08:46
I will try it
It's 2am here and I'll go to sleep but will try it when I wake up. Thank you @MadDV and @joeytwiddle I really appreciate it! :)
Jonathan
@MaddoxDevelopment
Oct 06 2016 08:50
good luck, if you haven’t checked out the mongoose docs on advanced queries, definately should. very helpful
Jenna Vuong
@jennabelle
Oct 06 2016 08:50
@MadDV I looked at the docs but couldnt find anything helpful. May you post the link to the Advanced Queries doc ?
Maybe I am missing something
Jonathan
@MaddoxDevelopment
Oct 06 2016 08:56
err, coulda sworn there was one, http://mongoosejs.com/docs/api.html#query-js but this is pretty good