当前位置:网站首页>Mongoose condition queries part of the data of the specified attribute
Mongoose condition queries part of the data of the specified attribute
2022-07-28 20:37:00 【Xiao language is scattered】
Mongoose Conditionally query part of the data of the specified attribute
I encountered a problem of data query when I was rolling out the code today , How to retrieve some specified values of an attribute in a database table , for example , Data sheet stable There is an attribute in it, which is status, Its value may be a,b,c,d, Now I just need to retrieve all the items in the table status The value of is a,b The data of , How to achieve ?
Previously, I wrote the function of retrieving some specified attributes in the database , Here is how to realize this requirement , Use mongoose Of model Of select Method , You can retrieve the required data , There are two ways of writing :
- model.select(“item1 item2 item3”);
stay select The attribute name to be retrieved is passed in the method , Just separate it with a space , This kind of writing is more direct , If you need to retrieve item1 Child attributes of a, You can write directly item1.a That's all right. , Such as model.select(“item1.a item2 item3”);
- model.select({item: 1, item2: 1, item: 0});
stay select Method to pass in an object ,1 Represents that this attribute needs to be retrieved ,0 Representing this attribute does not need to be retrieved , Properties that do not need to be retrieved do not need to be written .
These two ways of writing can be achieved , Depending on personal preference .
So how to realize the problem of the beginning of the article ,mongoose There is an interesting way to write ,“$”, It can realize some interesting functions .
$ in Can solve this problem , It means that one key corresponds to multiple values , for example model.find({status: {$in:[a,b]}}); What it does is retrieve all status The value of is a,b The data of .
$ nin We can also solve this problem , It means that a key does not correspond to the specified value , for example model.find({status: {$nin:[c,d]}}); It implements the retrieval of all status The value of the c,d The data of .
The two writing methods are applicable to different scenes , Their realizable functions are also different .
I also sorted out some other ways of writing , You can have a look if you need .
$lt Less than a certain value {age: {$lt: 30}}
$lte Less than or equal to a certain value {age: {$lte: 30}}
$gt Greater than a certain value {age: {$gt: 30}}
$gte Greater than or equal to {age: {$gte: 30}}
$eq be equal to {age: {$eq: 30}}
$ne It's not equal to {age: { $ne:24}}
$in A key corresponds to multiple values {age: {$in:[20,30]}}
$nin A key does not correspond to the specified value {age: {$nin:[20,30]}}
$or Multiple condition matching , Can be nested $in Use {$or: [{name: "name"}, {age: 28}]}
$nor Query documents that do not match a particular pattern
$exists Determine whether some key fields exist {name: {$exists: true}}
边栏推荐
猜你喜欢
随机推荐
DOS common commands
User and group and authority management
C语言数据 3(1)
[task03: complex query methods - views, subqueries, functions, etc.]
微信公众号授权登录后报redirect_uri参数错误的问题
MySQL startup error 1607 unexpected process termination
同质化代币与 NFT 结合,如何使治理结构设计更灵活?
Windows系统下Mysql数据库定时备份
【pytorch】LSTM神经网络
Wildcard ssl/tls certificate
C语言简单实例 1
LeetCode-297-二叉树的序列化与反序列化
Clock distribution of jesd204 IP core (ultrascale Series)
【CodeForces】Educational Codeforces Round 132 (Rated for Div. 2)
太空射击第11课: Sound and Music
3D激光SLAM:LeGO-LOAM论文解读---简介部分
[C语言刷题篇]链表运用讲解
[pytorch] LSTM neural network
字符设备驱动结构
Teach you how to draw a map with ArcGIS [thermal map]
![[C language] use function pointers to make a different calculator](/img/58/e6ba11e054d9e45ec979224ac3e4c6.png)




![[POC - proof of concept]](/img/57/0916e3711b27e2debfbdb9c9cb9713.png)



