Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...