各种查询 - MongoDB

发布于 2024-09-11 07:40:56 字数 539 浏览 10 评论 0原文

这是我的表格

unicorns =  {'name':'George',
             'actions':[{'action':'jump', 'time':123123}, 
                        {'action':'run', 'time':345345}, 
                        ...]}

如何执行以下查询?

  • 抓取所有独角兽的所有操作的时间,其中<​​strong> action=='jump' ?

  • 时间相等的情况下抓取所有独角兽的所有行动

    例如 {'action':'jump', 'time':123} 和 {'action':'stomp', 'time':123}


帮助会很棒 =)

This is my table:

unicorns =  {'name':'George',
             'actions':[{'action':'jump', 'time':123123}, 
                        {'action':'run', 'time':345345}, 
                        ...]}

How can I perform the following queries?

  • Grab the time of all actions of all unicorns where action=='jump' ?

  • Grab all actions of all unicorns where time is equal?

    e.g. {'action':'jump', 'time':123} and {'action':'stomp', 'time':123}


Help would be amazing =)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪#恶を代价 2024-09-18 07:40:56

对于第二个查询,您需要使用MapReduce,这可能会带来很大的麻烦。这会起作用:

map = function() {
    for (var i = 0, j = this.actions.length; i < j; i++) {
        emit(this.actions[i].time, this.actions[i].action);
    }
}
reduce = function(key, value_array) {
    var array = [];
    for (var i = 0, j = value_array.length; i < j; i++) {
        if (value_array[i]['actions']) {
            array = array.concat(value_array[i]['actions']);
        } else {
            array.push(value_array[i]);
        }
    }
    return ({ actions: array });
}

res = db.test.mapReduce(map, reduce)
db[res.result].find()

这将返回类似这样的内容,其中_id键是您的时间戳:

{ "_id" : 123, "value" : { "actions" : [ "jump" ] } }
{ "_id" : 125, "value" : { "actions" : [ "neigh", "canter" ] } }
{ "_id" : 127, "value" : { "actions" : [ "whinny" ] } }

不幸的是,mongo当前不支持从reduce函数返回数组,因此需要愚蠢的{ actions: [...]} 语法。

For the second query, you need to use MapReduce, which can get a big hairy. This will work:

map = function() {
    for (var i = 0, j = this.actions.length; i < j; i++) {
        emit(this.actions[i].time, this.actions[i].action);
    }
}
reduce = function(key, value_array) {
    var array = [];
    for (var i = 0, j = value_array.length; i < j; i++) {
        if (value_array[i]['actions']) {
            array = array.concat(value_array[i]['actions']);
        } else {
            array.push(value_array[i]);
        }
    }
    return ({ actions: array });
}

res = db.test.mapReduce(map, reduce)
db[res.result].find()

This would return something like this, where the _id keys are your timestamps:

{ "_id" : 123, "value" : { "actions" : [ "jump" ] } }
{ "_id" : 125, "value" : { "actions" : [ "neigh", "canter" ] } }
{ "_id" : 127, "value" : { "actions" : [ "whinny" ] } }

Unfortunately, mongo doesn't currently support returning an array from a reduce function, thus necessitating the silly {actions: [...]} syntax.

路还长,别太狂 2024-09-18 07:40:56

使用点分隔符号:

db.unicorns.find({'actions.action' : 'jump'})

时间类似:

db.unicorns.find({'actions.time' : 123})

编辑:如果要按时间对结果进行分组,则必须使用 MapReduce

Use dot-separated notation:

db.unicorns.find({'actions.action' : 'jump'})

Similarly for times:

db.unicorns.find({'actions.time' : 123})

Edit: if you want to group the results by time, you'll have to use MapReduce.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文