【问题标题】:Getting Values with MongoDB regex使用 MongoDB 正则表达式获取值
【发布时间】:2017-11-13 06:06:00
【问题描述】:

如何使用 MongoDB 正则表达式在字符串中获取 opts 的值?
我想用正则表达式搜索猫 name 并得到猫 opts

架构:

const childSchema = new Schema({
    type: String,
    id: { type: Number, required: true, unique: true },
    title: String,
    message_text: String,
    parse_mode: String,
    thumb_url: String,
    description: String,
});

const parentSchema = new Schema({
    _id: Number,
    name: String,
    opts: childSchema,
});

代码:

Mq.aggregate(
    [
        {
            "$match": {
                "name": { "$regex": "c", "$options": "i" }
            }
        },
        { "$unwind": "$name" },
        {
            "$match": {
                "name": { "$regex": "c", "$options": "i" }
            }
        },
        {
            "$group": {
                "_id": "$opts",
            }
        }
    ],
    function (err, results) {
        if (err) {
            console.log(err)
        } else {
            console.log(results)
        }
    }
)

输出:

[ { _id:
     { type: 'article',
       id: 2,
       title: 'persian cat',
       message_text: 'test',
       parse_mode: 'Markdown',
       thumb_url: 'http://www.immig-chicago.com/clientuploads/graphics/dvLottery_2a.jpg',
       description: 'des',
       _id: 5a011c3236bdcc2540747d0f } },
  { _id:
     { type: 'article',
       id: 1,
       title: 'cat',
       message_text: 'Hi',
       parse_mode: 'Markdown',
       thumb_url: 'http://www.immig-chicago.com/clientuploads/graphics/dvLottery_2a.jpg',
       description: 'des',
       _id: 59f7cf23ba668128fc48b8a7 } } ]

但我需要输出 opts 中的值,我的意思是这样的:

{ type: 'article',
       id: 2,
       title: 'persian cat',
       message_text: 'test',
       parse_mode: 'Markdown',
       thumb_url: 'http://www.immig-chicago.com/clientuploads/graphics/dvLottery_2a.jpg',
       description: 'des',
       _id: 5a011c3236bdcc2540747d0f },
{ type: 'article',
       id: 1,
       title: 'cat',
       message_text: 'Hi',
       parse_mode: 'Markdown',
       thumb_url: 'http://www.immig-chicago.com/clientuploads/graphics/dvLottery_2a.jpg',
       description: 'des',
       _id: 59f7cf23ba668128fc48b8a7 } 

更新:
我的项目中有let opts = [];,我想删除密钥并获取 opts 的值并将它们放入 let opts - [{},{},{},....]

我的数据:

   [ [ { _id: 0, name: 'dog', opts: [Object], __v: 0 },
        { _id: 1, name: 'cat', opts: [Object], __v: 0 },
        { _id: 2, name: 'persian cat', opts: [Object], __v: 0 } ] ]

【问题讨论】:

  • "name" 不是数组时,你为什么要做{ "$unwind": "$name" }?我想你的意思是{ "$unwind": "$opts" }。假设Mq 在这里实际上与parentSchema 相关,但各处的不同命名并没有真正的帮助。
  • 哦,好吧,我有 nameopts,我想在 name 中搜索猫的名字并在输出中选择猫
  • 所以??我的观点是{ "$unwind": "$name" } 应该抛出一个非常响亮的错误,因为它本身不是一个数组。无论是那个还是你的问题都是误导性的,你的实际数据与你在这里呈现的方式完全不同。所以你还没有真正“清理”刚刚问你的任何事情。
  • 我的输出没有任何错误!当我使用{ "$unwind": "$name" }, 时,但我删除了那条线。注意:名称是字符串,是我的猫的名字,我想让猫选择输出
  • 看不到你怎么还不明白,这没有任何意义。我想我对相关问题已经很清楚了。您的查询和提供的数据“不匹配”。所以除非他们这样做,否则真的不清楚你在这里问的是什么。

标签: javascript mongodb mongoose


【解决方案1】:

我肯定知道一件事你可以使用$projectMq.aggregate 来隐藏最终结果中的_id,方法是分配如下值:{ _id: 0 }

我还发现 this 可能会有所帮助;)

【讨论】:

  • 当我添加了{ $project: {_id: 0} }, 我得到了[ {}, {} ]
  • 不...绝对不是那样的。我只在 _id: 0 之后提到了如何使用它,您应该指定要投影的所有其他字段,如链接 { $project: {_id: 0, name: '$_id.name ',城市:'$_id.city',州:'$_id.state'} } 。如果你不能在我之前管理一下,我会尝试在我的电脑上带来完整的例子并调试你的代码
【解决方案2】:

解决了✔️

1:我使用正则表达式找到了我的猫 name
2: I Got Cats opts 价值 By .distinct()

var regex = new RegExp("c", "i"); //Case insensitive Match
Mq.find().distinct('opts', {name: regex}, function(err, docs) {
    console.log(docs)
})

输出:

    [{ type: 'article',
           id: 1,
           title: 'cat',
           message_text: 'Hi',
           parse_mode: 'Markdown',
           thumb_url: 'http://www.immig-chicago.com/clientuploads/graphics/dvLottery_2a.jpg',
           description: 'des',
           _id: 59f7cf23ba668128fc48b8a7 },
{ type: 'article',
           id: 2,
           title: 'persian cat',
           message_text: 'test',
           parse_mode: 'Markdown',
           thumb_url: 'http://www.immig-chicago.com/clientuploads/graphics/dvLottery_2a.jpg',
           description: 'des',
           _id: 5a011c3236bdcc2540747d0f } ]

【讨论】:

    猜你喜欢
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多