Results 1 to 4 of 4

Thread: And query document based on the same field in an array of json in java

  1. #1
    Join Date
    Oct 2011
    Posts
    9

    Default And query document based on the same field in an array of json in java

    Hi there,

    I'm a new user of mongodb and I'm stuck trying to translate in Java a mongoDB query.

    Here is the problem. I have a "test" collection such as :
    Code:
    db.test.find()
    { "_id" : ObjectId("4e9e9ec65bd7871ef40fc52b"), "a" : [ { "b" : "c" } ] }
    { "_id" : ObjectId("4e9e9edf5bd7871ef40fc52c"), "a" : [ { "b" : "c" }, { "b" : "d" } ] }
    { "_id" : ObjectId("4e9e9ee45bd7871ef40fc52d"), "a" : [ { "b" : "d" } ] }
    and I would like to query only the second document based on the value of the b field in the embedded documents in the array "a".

    In order to do so, I'm using the "$and" operand (on which I don't have found many information on the web).
    Code:
     db.test.find({$and : [{"a.b":"c"},{ "a.b":"d"}]})
    { "_id" : ObjectId("4e9e9edf5bd7871ef40fc52c"), "a" : [ { "b" : "c" }, { "b" : "d" } ] }
    I've searched about this query in Java but didn't find out how to use it. I'm using the spring-data-mongodb framework and querying the db through the use of a MongoTemplate object.

    Thanks for any answer

  2. #2
    Join Date
    Aug 2004
    Posts
    1,104

    Default

    We are still working on the $and support for the Query/Criteria classes. We added an and() method to the Query recently but we are also considering re-working the $and/$or support to be easier to use.

    See these issues for progress:
    https://jira.springsource.org/browse/DATADOC-283
    https://jira.springsource.org/browse/DATADOC-300
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  3. #3
    Join Date
    Oct 2011
    Posts
    9

    Default

    Ok. Thanks for this answer.

    This example is what I need.

    I've seen here that the RC1 release is due to monday so I'll wait until there.

  4. #4
    Join Date
    Aug 2004
    Posts
    1,104

    Default

    We've changed the release plans now and there will be an M5 release early next week to be followed by and RC1 release in a few weeks. The new $or/$nor/$and support for the Criteria class will be in M5.
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

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
  •