MongoDB array: remove or insert in one setence...

24 views
Skip to first unread message

themacboy

unread,
Jul 13, 2018, 11:40:01 AM7/13/18
to mongodb-user
Im trying to look if there is a way to remove or inser a element in a array in one one sentence.

Example:

["a", "b", "c"]

I like to sent a order that check if element is present in the array, if YES then remeves the element, if NOT then add the element.

Is that possible in a only one sentence?

Kevin Adistambha

unread,
Aug 2, 2018, 12:35:32 AM8/2/18
to mongodb-user

Hi

It’s been some time since you posted this question. Have you found a solution yet?

I like to sent a order that check if element is present in the array, if YES then remeves the element, if NOT then add the element.
Is that possible in a only one sentence?

If you mean is it possible to do logic-based insertion in MongoDB (either through a command or aggregation), then it is not currently possible.

Having said that, the mongo shell is a Javascript interpreter, so you may be able to do what you need using the shell (see the mongo shell and Write Scripts for the mongo Shell). Alternatively, you can write a small script using the various drivers.

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages