Arun Kumar
Arun Kumar

Reputation: 78

How to get a range of data in mongodb for a substring of field

I am having a field as build and another as data in my MongoDB. my build field is as b3312, b3313, b3313... I want to fetch data for a range of builds. can we add regex with it and gt in find query? or is there any other solution to this issue?

Upvotes: 0

Views: 179

Answers (2)

abrorkhamidov
abrorkhamidov

Reputation: 143

Try this one:

let build_ids = ["b3312","b3313","b3314"];
db.collectionName.find({build: {$in: build_ids}})

Upvotes: 0

Mohammad Yaser Ahmadi
Mohammad Yaser Ahmadi

Reputation: 5051

you can use

$lte

selects the documents where the value of the field is less than or equal to (i.e. <=) the specified value.

$gte

selects the documents where the value of the field is greater than or equal to (i.e. >=) a specified value

db.collectionName.find( { build : { $gte: "b3312", $lte :  "b3314"} } )

result:

/* 1 */
{
    "_id" : ObjectId("60118271fec3d8db92a39872"),
    "build" : "b3312"
}

/* 2 */
{
    "_id" : ObjectId("60118279fec3d8db92a3987e"),
    "build" : "b3313"
}

/* 3 */
{
    "_id" : ObjectId("6011827ffec3d8db92a3988b"),
    "build" : "b3314"
}

Upvotes: 1

Related Questions