2

I have a data:

{'_id': 0, 'values': [{'value': 1, 'value_type': 'type1'}, {'value': 2, 'value_type': 'type2'}, {'value': 134, 'value_type' : 'type1'}, {'value': 2564, 'value_type': 'type212'}]} 
{'_id': 1, 'values': [{'value': 136, 'value_type': 'type1'}, {'value': 465652, 'value_type': 'type1'}, {'value': 4, 'value_type' : 'type299'}, {'value': 112564, 'value_type': 'type2456'}]}

I'm working with pymongo 2.2 and Python 3.2.

The goal is to find the dictionary (value and value_type) which has value_type is equal to type1 and minimal value of value to remove them. In my case, that's {'value': 1, 'value_type': 'type1'} and {'value': 136, 'value_type': 'type1'} and I want to remove them.

How do I to achieve that?

--Edit--

I'm aware how to update (delete in my case), I'll be using $unset operator. What I'm unaware is how to find the values I need to remove.

Alan Coromano
  • 24,958
  • 53
  • 135
  • 205
  • @alexvassel -- "what have you tried?" is not helpful. This question has a specific answer, independent of anything the poster has tried. If you're looking for information about the solution because you don't know it, consult the documentation or your expertise. This question is about how to phrase a query. It would be like asking "what have you tried?" to someone asking "What is a working way to refer to the second building on the left in French?". – Purrell Nov 15 '12 at 15:51
  • @Purrell I can not see your answer. Text that you typed will not solve the problem. – alexvassel Nov 15 '12 at 15:54
  • @Purrell I don't think that we should talk about this anymore. Have a good day. – alexvassel Nov 15 '12 at 15:57

2 Answers2

2

use a $pull update query http://www.mongodb.org/display/DOCS/Updating#Updating-%24pull

coll.update({}, {"$pull" :{"values": {"value":1}})
jassinm
  • 7,323
  • 3
  • 33
  • 42
1

Use the $pull operator like this:

value = 150
coll.update({}, 
    {'$pull': {'values': {'value_type': 'type1', 'value': {'$lt': value}}}},
    multi=True)

Setting the multi parameter to True tells the update to update all documents, not just the first one.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • I don't know the value, each item has their own minimal value. – Alan Coromano Nov 15 '12 at 16:24
  • @AlanDert Sorry, I misunderstood your question. If you need to remove the element with the minimum value, there's no set operation to do that so you'll need to iterate over the collection and manually update each item to remove that element in code. – JohnnyHK Nov 15 '12 at 16:43
  • @AlanDert See [this answer](http://stackoverflow.com/a/13407073/1259510) to a similar question. – JohnnyHK Nov 15 '12 at 22:32