Results 1 to 3 of 3

Thread: Mongo criteria querying twice the same field - exception

  1. #1
    Join Date
    Jul 2012
    Posts
    2

    Default Mongo criteria querying twice the same field - exception

    I am trying to query mongodb using spring. We have a collection which holds a tree and a include a list of items as a tree path (so we can easily traverse the tree). We have a query which needs to return all child nodes of a specific node. Our query is based on selecting all nodes that have the node (parent) in the path and are one level lower than the parent (level). Our criteria is as follows:

    Criteria.where("treePath").in(parentId).and("treeP ath").size(level)

    Alas, when we call this on mongodb we get the following exception:

    org.springframework.data.mongodb.InvalidMongoDbApi UsageException: Due to limitations of the com.mongodb.BasicDBObject, you can't add a second 'treePath' expression specified as 'treePath : { "$size" : 2}'. Criteria already contains 'treePath : { "$in" : [ "50137df5f49f9b4a6481d639"]}'.

    Are there other suggestions on how to achieve the same? One option I was thinking of was to query mongodb directly. I tried

    String command = "{findAndModify:\"Task\",query:{$and:[{treePath:\"5013a79a36600872ecf4dba8\"},{treePath: {$size:2}},{order:{$gte:0}}]},update:{$inc:{order:1}}}";
    CommandResult commandResult = mongoTemplate.executeCommand(command);

    But this will only update the first record and I need them all updated.

    Thanks!

  2. #2
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    492

    Default

    Use the ….andOperator(…) which mimics the $and MongoDB operator which allows defining multiple criterias for a single key.

  3. #3
    Join Date
    Jul 2012
    Posts
    2

    Default

    Yes. It works! Thanks!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •