Asked  7 Months ago    Answers:  5   Viewed   35 times

Here is array structure

contact: {
    phone: [
        {
            number: "+1786543589455",
            place: "New Jersey",
            createdAt: ""
        }
        {
            number: "+1986543589455",
            place: "Houston",
            createdAt: ""
        }

    ]
}

Here I only know the mongo id(_id) and phone number(+1786543589455) and I need to remove that whole corresponding array element from document. i.e zero indexed element in phone array is matched with phone number and need to remove the corresponding array element.

contact: {
    phone: [
        {
            number: "+1986543589455",
            place: "Houston",
            createdAt: ""
        }
    ]
}

I tried with following update method

collection.update(
    { _id: id, 'contact.phone': '+1786543589455' },
    { $unset: { 'contact.phone.$.number': '+1786543589455'} }
);

But it removes number: +1786543589455 from inner array object, not zero indexed element in phone array. Tried with pull also without a success.

How to remove the array element in mongodb?

 Answers

95

Try the following query:

collection.update(
  { _id: id },
  { $pull: { 'contact.phone': { number: '+1786543589455' } } }
);

It will find document with the given _id and remove the phone +1786543589455 from its contact.phone array.

You can use $unset to unset the value in the array (set it to null), but not to remove it completely.

Tuesday, June 1, 2021
 
juanrpozo
answered 7 Months ago
58

Use numpy.delete() - returns a new array with sub-arrays along an axis deleted

numpy.delete(a, index)

For your specific question:

import numpy as np

a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
index = [2, 3, 6]

new_a = np.delete(a, index)

print(new_a) #Prints `[1, 2, 5, 6, 8, 9]`

Note that numpy.delete() returns a new array since array scalars are immutable, similar to strings in Python, so each time a change is made to it, a new object is created. I.e., to quote the delete() docs:

"A copy of arr with the elements specified by obj removed. Note that delete does not occur in-place..."

If the code I post has output, it is the result of running the code.

Sunday, June 6, 2021
 
GGio
answered 6 Months ago
76

You don't need the full notation as the placeholder has already moved to that position in the array.

db.junk.update(
    { "commandes.voyagesSouscrits.idVoyage": "123" },
    {$pull: { "commandes.$.voyagesSouscrits": { idVoyage: "123" } }}
)

This part:

idVoyage: { <query> }

is only needed because the positional operator in "commandes.$.voyagesSouscrits" can only match the first array position found in the query.

http://docs.mongodb.org/manual/reference/operator/projection/positional/

Hope that clears it up.

Friday, August 6, 2021
 
dmp
answered 4 Months ago
dmp
80

You need to apply np.delete along an axis. Please refer to the third example in the documentation.

Y = np.delete(X, 1, axis=1)
Monday, August 23, 2021
 
user505210
answered 4 Months ago
83

TL;DR:

Use the async driver if the operations are slow, or use the regular driver in most cases. You shouldn't use the core driver.

MongoDB Regular Driver:

General driver that you can use to search, create, read, update and delete documents. The find(...), updateMany(...), deleteMany(...) and similar methods will hang for as long as the result is not returned or the operation not done (synchronous behavior). This is the driver that most program uses and is good in most cases.

Here is an example for inserting a single Document:

collection.insertOne(doc);
//Do something here.
System.out.println("Inserted!")

MongoDB Async Driver:

Another type of driver that you can use to search, create, read, update and delete documents. This driver offers similar methods than the regular driver (find(...), updateMany(...), deleteMany(...), etc.).

The difference with the regular driver is that the main thread will not hang because the async driver sends the result in a callback (asynchronous behavior). This driver is used when the operations can take a long time (a lot of data to go through, high latency, query on unindexed fields, etc.) and you do not want to manage multiple threads.

Here is an example of the callback when inserting a single Document:

collection.insertOne(doc, new SingleResultCallback<Void>() {
    @Override
    public void onResult(final Void result, final Throwable t) {
        //Do something here.
        System.out.println("Inserted!");
    }
});
// Do something to show that the Document was not inserted yet.
System.out.println("Inserting...")

For more informations, read this.

MongoDB Core Driver

Base layer of the regular and async drivers. It contains low-level methods to do all the operations common to the regular and async drivers. Unless you are making a new API / Driver for MongoDB, you shouldn't use the core driver.

Tuesday, November 2, 2021
 
Industrial
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share