0

I am trying to remove document from array using reactive mongo Query DSL. My document structure is as below:

{
"_id" : ObjectId("55950666c3ad8c860b5141cc"),
"name" : "Texla",
-------------------------
 "location" : {
    "_id" : ObjectId("5595074bc3ad8c19105141d0"),
    "companyFieldId" : ObjectId("559140f1ea16e552ac90e058"),
    "name" : "Location",
    "alias" : "Site",
    "locations" : [
        {
            "_id" : ObjectId("5595074bc3ad8c19105141d1"),
            "country" : "India",
            "state" : "Punjab",
            "city" : "Moga",
            "zip" : "142001",
            "custom" : false
        },
        {
            "_id" : ObjectId("5595074bc3ad8c19105141d2"),
            "country" : "India da address",
            "custom" : true
        }
    ]
},
------------------------
}

My query using "Reactive Mongo Extensions" is :

genericCompanyRepository.update($doc("_id" $eq BsonObjectId.apply("55950666c3ad8c860b5141cc")), $pull("location.locations" $elemMatch("_id" $eq BsonObjectId.apply("5595074bc3ad8c19105141d1"))), GetLastError(), false, false);

With above update statement. there is not document is pull from array. What is the problem with Query ?

When i run below function, this will work.

genericCompanyRepository.findOne($doc("location.locations" $elemMatch("_id" $eq BsonObjectId.apply("5595074bc3ad8c19105141d1")))) function, the element is return. 
Harmeet Singh Taara
  • 6,483
  • 20
  • 73
  • 126

2 Answers2

1

I am using Play-ReactiveMongo and update query in mongo syntax as below :

db.collectionName.update({"_id" : ObjectId("55950666c3ad8c860b5141cc"),
                         "location.locations":{"$elemMatch":{"_id":ObjectId("5595074bc3ad8c19105141d1")}}},
                         {"$pull":{"location.locations":{"_id":ObjectId("5595074bc3ad8c19105141d1")}}})

And this query in salat mongo as below :

val query = List(MongoDBObject("_id" -> new ObejectId("55950666c3ad8c860b5141cc"),
                         "location.locations" -> MongoDBObject("$elemMatch" -> MongoDBObject("_id"-> new ObjectId("5595074bc3ad8c19105141d1")),MongoDBObject("$pull"-> MongoDBObject("location.locations" -> MongoDBObject("_id": new ObjectId("5595074bc3ad8c19105141d1"))))

and update this in collection using as below :

db.collectionName.update(query)

I am not using reactive Mongo directly but after looking your code for update query you should changed syntax as below, please note below query syntax not tested.

genericCompanyRepository.update($doc("_id" $eq BsonObjectId.apply("55950666c3ad8c860b5141cc")
  "location.locations" $elemMatch("_id" $eq BsonObjectId.apply("5595074bc3ad8c19105141d1")), 
$pull("location.locations"("_id" $eq BsonObjectId.apply("5595074bc3ad8c19105141d1")),
GetLastError(), false, false);
Neo-coder
  • 7,715
  • 4
  • 33
  • 52
  • thanks @yogesh but with this code there is an issue `genericCompanyRepository.update($doc("_id" $eq BsonObjectId.apply("55950666c3ad8c860b5141cc") "location.locations" $elemMatch("_id" $eq BsonObjectId.apply("5595074bc3ad8c19105141d1")), pull("location.locations"("_id" $eq BsonObjectId.apply("5595074bc3ad8c19105141d1")), GetLastError(), false, false);` the problem with `$doc("_id" $eq BsonObjectId.apply("55950666c3ad8c860b5141cc") "location.locations" $elemMatch("_id" $eq BsonObjectId.apply("5595074bc3ad8c19105141d1"))` statement. – Harmeet Singh Taara Jul 02 '15 at 15:18
  • @HarmeetSinghTaara this query not tested using `reactive Mongo` so It may you should convert mongo query to `reactivemongo` I will try using `reactive mongo` and update very soon, so in mean time you should try yourself :) – Neo-coder Jul 02 '15 at 15:23
  • @HarmeetSinghTaara no worry :) – Neo-coder Jul 03 '15 at 06:21
1

With the help of Above @yogesh answer i resolve my problem, following is my updated query.

genericCompanyRepository.update($doc("_id" $eq BsonObjectId.apply("55950666c3ad8c860b5141cc"), "location.locations" $elemMatch("_id" $eq BsonObjectId.apply("5595074bc3ad8c19105141d1"))), 
      $pull("location.locations" $eq $doc("_id" $eq BsonObjectId.apply("5595074bc3ad8c19105141d1"))), GetLastError(), false, false);
Harmeet Singh Taara
  • 6,483
  • 20
  • 73
  • 126