Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
870 views
in Technique[技术] by (71.8m points)

mongoose - 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?

question from:https://stackoverflow.com/questions/65921189/how-to-get-a-range-of-data-in-mongodb-for-a-substring-of-field

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.7k users

...