【问题标题】:How to convert mongo ObjectId .toString without including 'ObjectId()' wrapper -- just the Value?如何在不包含'ObjectId()'包装器的情况下转换mongo ObjectId .toString - 只是值?
【发布时间】:2016-12-15 19:58:35
【问题描述】:

我要解决的是:使用此建议的方法 (mapReduce) 使用 $in 保留我的 Id 数组的顺序: Does MongoDB's $in clause guarantee order

我已经完成了作业,发现将它们转换为字符串是理想的: 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 不断返回的内容:

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

这让我相信它无法将其中的 ObjectId 与 dataIds 的索引相匹配。但是,如果我只在 .find() 中使用 $in 子句,则会返回正确的产品——但顺序不正确。

更新:

得到意外的行为。

  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。

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

【问题讨论】:

    标签: javascript node.js mongodb mongoose mapreduce


    【解决方案1】:

    想通了:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-12
      • 1970-01-01
      • 2016-12-02
      • 2015-11-23
      • 2016-02-21
      • 2018-02-13
      • 2022-01-23
      相关资源
      最近更新 更多