2

I am a mongoDB noob and I'm struggling with a query. Let's assume I have the following structure:

{
    "id": "1",
    "type": 2,
    "shops": ["1000","2000","3000"]
  },
  {
    "id": "2",
    "type": 2,
    "shops": ["1000","4000","5000"]
  }

How do I remove every shop which is equal to, e.g 1000, so that the resulting db records would be:

{
    "id": "1",
    "type": 2,
    "shops": ["2000","3000"]
  },
  {
    "id": "2",
    "type": 2,
    "shops": ["4000","5000"]
  }

Thanks so much in advance!

Poider12
  • 223
  • 3
  • 15
  • The updated json `"shops": [ 0: 4000 1: 5000 ]` seems incorrect to me. The list shall have some object within it to contain those keys and values. – Naman Aug 22 '17 at 03:53

1 Answers1

2

You can execute something like this using the pull operator as follows:

db.shopCollection.update(
    { },
    { $pull: { "shops": { "idShop", 1000 } } },
    { multi: true }
)

assuming shopCollection as the name of your collection.

Edit: To make use of the same in java, you can follow the answers as mentioned in MongoDB Java pull. In java, something like this:

Bson query = new org.bson.Document();
Bson fields = new org.bson.Document().append("shops", new org.bson.Document().append("$idShop",1000));
Bson update = new org.bson.Document("$pull",fields);
yourCollection.updateMany(query, update);

Edit2: For the update JSON value shared you can update the java code to:

Bson filter = Filters.in("shops","1000");
Bson query = Filters.elemMatch("shops", filter);
Bson update = new org.bson.Document("$pull", filter); 

campaignsCollection.updateMany(query, update);` 
Naman
  • 27,789
  • 26
  • 218
  • 353
  • 1
    Thanks so much for answering so quickly. I get the collection by calling getCollection("shops"), but I can't find the "update" method, only updateOne and updateMany. – Poider12 Aug 21 '17 at 19:32
  • 1
    @Poider12 Please update the question with the java code you're using to access the collection. Also note the query shared by me was for the mongo shell and not Java. You can `updateMany` in java to update the list of documents. – Naman Aug 21 '17 at 19:35
  • Bson query = new Document(); Bson fields = new Document().append("spots", new Document().append("$elemMatch",1000)); Bson update = new Document("$pull",fields); campaignsCollection.updateMany(query, update); Should be something like this? – Poider12 Aug 21 '17 at 20:26
  • 1
    @Poider12 Ya somewhere like that. Updated the answer, also do recheck your updated Json in the question. – Naman Aug 22 '17 at 03:55
  • Ok. But what do I use instead of "$idShop"? It's an array so there is just an array index – Poider12 Aug 22 '17 at 12:42
  • 1
    @Poider12 Could you update the question with a correct json in that case. Would infer you objects from it. – Naman Aug 22 '17 at 12:43
  • I was able to remove it using: Bson query = new Document(); Bson fields = new org.bson.Document().append("shops", "1000"); Bson update = new Document("$pull",fields); campaignsCollection.updateMany(query, update); ...but now it shows "Cannot apply $pull to a non-array value" (although the behaviour was the I wanted). Any thoughts? – Poider12 Aug 22 '17 at 15:53
  • 1
    @Poider12 Edited the answer again, That might help you. Also could you share the java model you are using for your documents. – Naman Aug 22 '17 at 17:05
  • Thanks, the thing is there is no field "shop"...just an array "shops" – Poider12 Aug 22 '17 at 17:44
  • @Poider12 that was a typo. It should be fieldname itself. – Naman Aug 22 '17 at 17:54