Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB: Find the minimum element in array and delete it

I have a documents in MongoDB, one of them looks like this:

{
"_id" : 100,
"name" : "Something",
"items" : [
    {
        "item" : 47,
        "color" : "red"
    },
    {
        "item" : 44,
        "color" : "green"
    },
    {
        "item" : 39,
        "color" : "blue"
    }
]
}

In every document I need to find the minimum item and delete it. So it should be like this:

{
"_id" : 100,
"name" : "Something",
"items" : [
    {
        "item" : 47,
        "color" : "red"
    },
    {
        "item" : 44,
        "color" : "green"
    }
]
}

It looks like findAndModify function should be used here but I can't go any further.

How to find the minimum element in array and delete it?

I'm using MongoDB and Pymongo driver.

like image 418
megas Avatar asked Nov 11 '12 20:11

megas


Video Answer


2 Answers

If you are not restricted to having the query be in one single step, you could try:

step 1) use the aggregate function with the $unwind and $group operators to find the minimum item for each document

myresults = db.megas.aggregate( [ { "$unwind": "$items" },  
    {"$group": { '_id':'$_id' , 'minitem': {'$min': "$items.item" } } } ] )

step 2) the loop through the results and $pull the element from the array

for result in myresults['result']:
    db.megas.update( { '_id': result['_id'] }, 
        { '$pull': { 'items': { 'item': result['minitem'] } } } )
like image 158
Kay Avatar answered Sep 17 '22 23:09

Kay


Please find my solution written in plain javascript. It should work straight through MongoDb shell

cursor = db.students.aggregate(
[{ "$unwind": "$items" }, 
 { "$match": { "items.color": "green"}},
 { "$group": {'_id': '$_id', 'minitem': {
                '$min': "$items.item"
            }
        }
    }

]);

cursor.forEach(
function(coll) {
    db.students.update({
        '_id': coll._id
    }, {
        '$pull': {
            'items': {
                'item': coll.minitem
            }
        }
    })
})
like image 32
Faktor 10 Avatar answered Sep 17 '22 23:09

Faktor 10