0
model
state: [ {
        type: String,
        require: true
    }]

states = ['Gujarat','Rajasthan'];
state.findOneAndupdate({'_id':id},{state:states});

how can I update if exist override;

i want store in model is

states:['Gujarat','Rajstan','Amreli']
2
  • 1
    Please add some details to your question. Commented Apr 11, 2020 at 15:49
  • add some model detail please answer me Commented Apr 11, 2020 at 15:57

1 Answer 1

1

An array field update (uniquely) in mongodb can be achieved using $addToSet with $each modifier:

Query:

let states = ["Gujarat", "Rajasthan"];

db.state.update({ _id: id }, { $addToSet: { state: { $each: states } } });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, you save my time. contact no 9033325096 whatsapp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.