MongoDB中$ in的结果顺序,例如MySQL字段('_id',...) [英] Order of results in MongoDB with $in like MySQL field('_id', ...)

查看:760
本文介绍了MongoDB中$ in的结果顺序,例如MySQL字段('_id',...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MongoDB构建一个非常漂亮的趋势"帖子算法.由于该算法会消耗大量时间,因此我在cron任务中执行了我的算法,然后缓存了帖子ID的排序数组.像这样(PHP vardump):

"trending" => array("548ac5ce05ea675e468b4966", "5469c6d5039069a5030041a7", ...)

要点是,我找不到使用MongoDB以此顺序检索它们的任何方法. 使用MySQL只需完成以下操作即可:

SELECT * FROM posts
ORDER BY FIELD(_id, '548ac5ce05ea675e468b4966', '5469c6d5039069a5030041a7',...)

到目前为止,我尝试的是可以找到的东西

有人做到了吗,甚至不知道从哪里开始?

谢谢!

解决方案

我来自您链接的SO帖子.我发现是使用MongoDB提供的 $$ ROOT 变量. /p>

我的代码如下:

var ids = [456, 123, 789]
  , stack = []
  ;

// Note that `i` is decremented here, not incremented like
// in the linked SO post. I think they have a typo.
for (var i = ids.length-1; i > 0; i--) {
  var rec = {
    $cond: [
      {$eq: ['$_id', ids[i - 1]]},
      i
    ]
  };

  if (stack.length === 0) {
    rec.$cond.push(i + 1);
  }
  else {
    var lval = stack.pop();
    rec.$cond.push(lval);
  }

  stack.push(rec);
}

var pipeline = [
  {$match: {
    _id: {$in: ids}
  }},
  {$project: {
    weight: stack[0],
    data: '$$ROOT' // This will give you the whole document.
  }},
  {$sort: {weight: 1}}
];

Posts.aggregate(pipeline, function (err, posts) {
  if (err) return next();

  // Use Underscore to grab the docs stored on `data` from above.
  res.json(_.pluck(posts, 'data'));
});

请注意,我个人不确定该for循环中用于构建查询的情况. :-P因此,大多数功劳应该归功于他们.另外我还不确定是否将虚拟字段(如果您使用的是Mongoose,就象我一样)包含在此处,但我怀疑没有.

I'm building a quite fancy "trending" posts algorithm using MongoDB. As the algorithm consumes quite a lot of time, I'm executing my algorithm in a cron task and then I cache a sorted array of posts ids. Like this (PHP vardump):

"trending" => array("548ac5ce05ea675e468b4966", "5469c6d5039069a5030041a7", ...)

The point is that I can not find any way to retrieve them in that order using MongoDB. Using MySQL it would be done just by doing:

SELECT * FROM posts
ORDER BY FIELD(_id, '548ac5ce05ea675e468b4966', '5469c6d5039069a5030041a7',...)

What I tried so far is what it can be founde here, so now I am able to retrieve an sorted list with the weight, but not the posts. The response is like this:

{
    "result" : [ 
        {
            "_id" : ObjectId("548ac5ce05ea675e468b4966"),
            "weight" : 1
        }, 
        {
            "_id" : ObjectId("5469c6d5039069a5030041a7"),
            "weight" : 2
        }
    ], 
    "ok" : 1
}

Does anyone has achieved this or even know where to start?

Thank you!

解决方案

I'm coming from the SO post you linked. What I found was to use the $$ROOT variable provided by MongoDB.

My code looks like this:

var ids = [456, 123, 789]
  , stack = []
  ;

// Note that `i` is decremented here, not incremented like
// in the linked SO post. I think they have a typo.
for (var i = ids.length-1; i > 0; i--) {
  var rec = {
    $cond: [
      {$eq: ['$_id', ids[i - 1]]},
      i
    ]
  };

  if (stack.length === 0) {
    rec.$cond.push(i + 1);
  }
  else {
    var lval = stack.pop();
    rec.$cond.push(lval);
  }

  stack.push(rec);
}

var pipeline = [
  {$match: {
    _id: {$in: ids}
  }},
  {$project: {
    weight: stack[0],
    data: '$$ROOT' // This will give you the whole document.
  }},
  {$sort: {weight: 1}}
];

Posts.aggregate(pipeline, function (err, posts) {
  if (err) return next();

  // Use Underscore to grab the docs stored on `data` from above.
  res.json(_.pluck(posts, 'data'));
});

Note that I'm personally not exactly sure of what's going on in that for loop to build the query. :-P So most credit should go to them. Also I'm not sure yet if virtual fields (if you're using Mongoose, as I am) would be included here, but I suspect not.

这篇关于MongoDB中$ in的结果顺序,例如MySQL字段('_id',...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆