mongodb使用$switch做条件处理:
''''var userAccess = [ "Book", "Journal article", "Conference paper"];
db.all1000.aggregate([
{
$project:{
"three_type": 1, //想要保留的字段(three_type)
"citation": //想要保留的字段2(是对字段(cited_by)进行分类后生成的新字段)
{
$switch: { //类似if-else if-esle的分类器
branches: [
{
case: { $and : [ { $gte : [ { $max : "$cited_by" }, 0 ] },
{ $lte : [ { $max : "$cited_by" }, 10 ] } ] }, //条件1
then: "1" //对应的结果
},
{
case: { $and : [ { $gt : [ { $max : "$cited_by" }, 10 ] },
{ $lte : [ { $max : "$cited_by" }, 50 ] } ] },
then: "2"
},
{
case: { $and : [ { $gt : [ { $max : "$cited_by" }, 50 ] },
{ $lte : [ { $max : "$cited_by" }, 100 ] } ] },

本文介绍了如何在MongoDB中利用$switch进行条件判断处理,特别是针对cited_by字段,无论是数组还是单个对象,都能灵活应用$switch进行操作。示例中展示了从数组中提取最大值并根据条件返回不同结果的方法。
最低0.47元/天 解锁文章
1701

被折叠的 条评论
为什么被折叠?



