0

How i can pass multiple parameters in mongodb

My current setup is like this:

const query = (...query) => {

let newQuery = query // this is an array
    Model.findByIdAndUpdate(newQuery) // nothing happen coz the value is array 
}

query(id, {name:'json bourne'})

I want to pass the all the parameters on the query. Thanks

The original query is this Model.findByIdAndUpdate(id, { name: 'jason bourne' })

Is to pass the parameters like this

let newQuery = id, { name: 'jason bourne'}// this will not work also it on get the first item which is id

Model.findByIdAndUpdate(newQuery)

6
  • Why not just { id, name: 'json bourne'} ? Commented Aug 22, 2019 at 11:23
  • That will not work as id, and name is a separate object see mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate Commented Aug 22, 2019 at 11:28
  • Your question is not self explanatory please explain it better so that we can help you out Commented Aug 22, 2019 at 11:29
  • Here is the original query Model.findByIdAndUpdate(id, { name: 'jason bourne' }) Commented Aug 22, 2019 at 11:32
  • So tell me what exactly do you want to do with this Commented Aug 22, 2019 at 11:32

3 Answers 3

1

You destruct the array to get what you want

const query = (...query) => {

let [id, update, options = {}] = query // this is an array
Model.findByIdAndUpdate(id, update, options) // nothing happen coz the value is array

}

But in this case, it will be equivalent to

const query = (id, update, options = {}) => {
Model.findByIdAndUpdate(id, update, options) // nothing happen coz the value is array
}

Which is, again, equivalent to

const query = Model.findByIdAndUpdate;

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. This will work if you know and how many you are passing. What if you have not just two or three parameters.
well, Model.findByIdAndUpdate() takes 3 params and you have to pass at least two of them. thats why on my code, I am failing back the 3rd param to default {}
What i did so far is this
0

Model.findByIdAndUpdate requires atleast 2 arguments. First is the id itself which is required to find the document to update. And second argument is the options which is an object which contains the key-pair values of the fields that you want to update in that document. Here the key will be the field that needs to be updated and its value will be the value you wish the field to have. You can have as many key-pairs as you want as long as they are comma separated.

Comments

0

You could just spread the array:

 Model.findByIdAndUpdate(...newQuery)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.