0

I have some data in MongoDB, the data structure is like

{"a":{"b":[1,2,3,4,5],"c":[a,b,c,d,e]}}

1--->a
2--->b
3--->c
4--->d
5--->e

I want get the right part value by the left like search b by 2

How can I do this Thank you very much

5
  • what are you using to query your database? mongo shell? Commented May 18, 2015 at 14:07
  • what is the purpose of the b array? Am I getting it correct: you have some value X which has the index i inside b field. You want to get the ith element of c array? Commented May 18, 2015 at 14:09
  • Correct ! That is what I want do Commented May 18, 2015 at 14:13
  • I'm afraid this is not possible to achieve in an easy way, if it is possible at all. Are you bound to this structure or you can modify your model? Commented May 18, 2015 at 14:18
  • Well, thank you very much, The real data of b field is like 101.321, I set the b field as key and c field as value, but MongoDB seems like doesn't support key contains dot(.) Commented May 18, 2015 at 14:27

1 Answer 1

1

What I would suggest is to modify your structure to be able to make a query you want. My suggested structure is the following:

{
   a: [
         {b: 1, c: "a"},
         {b: 2, c: "b"},
               .
               .
               .
      ]
}

Then having your b value, you will be able to get the c one:

db.yourCollection.find({"a.b": "your_b_value"})

UPDATE by request of the question author

To make your query by range execute the following query:

db.yourCollection.find({"a.b": {$gte: 400.0}, "a.b": {$lte: 400.3}})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.