Announcement Announcement Module
Collapse
No announcement yet.
Mongo criteria querying twice the same field - exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Use the .andOperator() which mimics the $and MongoDB operator which allows defining multiple criterias for a single key.

    Comment


    • #3
      Yes. It works! Thanks!

      Comment

      Working...
      X