本文介绍了如何在不包含' ObjectId()'的情况下转换mongo ObjectId .toString包装器-只是价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要解决的是:使用建议的方法(mapReduce)使用$ in保留ID数组的顺序:MongoDB 的 $in 子句是否保证顺序

What I'm trying to solve is: preserving the order of my array of Ids with $in using this suggested method (mapReduce): Does MongoDB's $in clause guarantee order

我已经完成作业,发现将它们转换为字符串是理想的:比较猫鼬_id和字符串.

I've done my homework, and saw it's ideal to convert them to strings: Comparing mongoose _id and strings.

代码:

var dataIds = [ '57a1152a4d124a4d1ad12d80',
     '57a115304d124a4d1ad12d81',
     '5795316dabfaa62383341a79',
     '5795315aabfaa62383341a76',
     '57a114d64d124a4d1ad12d7f',
     '57953165abfaa62383341a78' ];

CollectionSchema.statics.all = function() {

      var obj = {};

      //adds dataIds to obj.scope as inputs , to be accessed in obj.map
      obj.scope = {'inputs': dataIds};

      obj.map = function() {
        //used toString method as suggested in other SO answer, but still get -1 for Id.
        var order = inputs.indexOf(this._id.toString());
        emit(order, {
          doc : this
        });
      };

      obj.reduce = function() {};

      obj.out = {inline: 1};

      obj.query = {"_id": {"$in": dataIds } };

      obj.finalize = function(key, value) {
        return value;
      };

      return Product
        .mapReduce(obj)
        .then(function(products){
          console.log('map products : ', products)
        })
     };

这就是我一直在找回console.log的产品承诺:

This is what I keep getting back for my console.log in the products promise :

[{ _id: -1, value: null } ]

哪个,使我相信它不能将其中的ObjectId与dataIds的索引进行匹配.但是,如果我只在.find()中使用$ in子句,则将返回正确的乘积,但是顺序不正确.

Which, leads me to believe it's not able to match the ObjectId from this, with an index of dataIds. However, if I just use the $in clause within a .find(), the correct products are returned -- but, in the incorrect order.

由此产生意外行为.

  obj.map = function() {
    for(var i = 0; i < inputs.length; i++){
      if(inputs[i] == this._id.toString()){
      }
      emit(inputs[i], this);
    }
  };

发射:

 [ { _id: '5795315aabfaa62383341a76', value: null },
  { _id: '57953165abfaa62383341a78', value: null },
  { _id: '5795316dabfaa62383341a79', value: null },
  { _id: '57a114d64d124a4d1ad12d7f', value: null },
  { _id: '57a1152a4d124a4d1ad12d80', value: null },
  { _id: '57a115304d124a4d1ad12d81', value: null } ]


  obj.map = function() {
    for(var i = 0; i < inputs.length; i++){
      if(inputs[i] == this._id.toString()){
        var order = i;
      }
      emit(this._id.toString(), this);
    }
  };

发射:

[ { _id: 'ObjectId("5795315aabfaa62383341a76")', value: null },
  { _id: 'ObjectId("57953165abfaa62383341a78")', value: null },
  { _id: 'ObjectId("5795316dabfaa62383341a79")', value: null },
  { _id: 'ObjectId("57a114d64d124a4d1ad12d7f")', value: null },
  { _id: 'ObjectId("57a1152a4d124a4d1ad12d80")', value: null },
  { _id: 'ObjectId("57a115304d124a4d1ad12d81")', value: null } ]

现在,如何摆脱ObjectId()包装器?最好是比str.slice()更干净的方法,它会起作用-但是,我觉得必须有一种更"mongo"/更安全的方法来转换ID.

Now, how do I get rid of the ObjectId() wrapper? Preferably, something more clean than str.slice(), which would work -- However, I feel there must be a more "mongo" / safer way of converting the Id.

我签出了文档,但只提到了toString()方法,该方法在地图中似乎无法正常工作: https://docs.mongodb.com/manual/reference/method/ObjectId.toString/

I checked out the docs, but it only mentions the toString() method, which does not seem to be working correctly within map: https://docs.mongodb.com/manual/reference/method/ObjectId.toString/

推荐答案

将其弄清楚了:

  obj.map = function() {
    for(var i = 0; i < inputs.length; i++){
      if(this._id.equals(inputs[i])) {
        var order = i;
      }
    }
    emit(order, {doc: this});
  };

这篇关于如何在不包含&amp;#39; ObjectId()&#39;的情况下转换mongo ObjectId .toString包装器-只是价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 13:39