1
{
prediction:{
        B-experience:["5"]    
           }
       }

Here,I need to find the B-experience is greater than and less than by some min and max values.In my document B-experience is in array of string. So i have to convert that array string in to int and aggregate match by expr with "gt" and "lt". I have tried with so many methods. But not getting desired result. Can some could help me out.

1

1 Answer 1

1

You can use $arrayElemAt to get the string out of the array, and $toInt in order to cast the string to int.

db.collection.aggregate([
  {
    $project: {
      predictionBexperience: {
        "$arrayElemAt": [
          "$prediction.B-experience",
          0
        ]
      }
    }
  },
  {
    $project: {
      predictionBexperience: {
        "$toInt": "$predictionBexperience"
      }
    }
  },
  {
    $match: {
      predictionBexperience: {
        $gt: 2,
        $lte: 7
      }
    }
  }
])

As can be seen on the playground with some more sample data.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the response. Its worked for me. I have one more doubt.I need full text search for two different fields in a collection. As I know, Mongocompass wont allow to text search for two fields in a single collection. So I created duplicate collection and added text search index for two fields in separate collections. So i need to write a query based on text for two fields. How can i use two text search of two collections in a single query.
You can use a $lookup to join these two collections, but the issue of full text search index is an issue for another question. It sound like there should be better solutions
Yes, but I m giving scoring for those two fields. For scoring, Only option I know is full text search. Is there any other solution I can get sir?
Hi sir, please look in to this question. stackoverflow.com/questions/72710360/…

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.