使用数组中的值更新MongoDB集合

时间:2017-09-28 15:47:02

标签: javascript arrays mongodb

我需要使用数组中的值更新MongoDB集合中的所有文档。

比如说我在mongo中有以下模式的文档:

{
  "key":"value",
  "key2":"value2",
  "keyN":"test"
}
{
  "key":"value",
  "key2":"value2",
  "keyN":"test2"
}
{
  "key":"value",
  "key2":"value2",
  "keyN":"test"
}

我有一个数组:[“test”,“test2”]

我想通过数组创建一个迭代器,因此集合中所有文档中的“keyN”键都使用“test”或“test2”进行更新

which php

我该怎么做?我尝试使用forEach,但我无法弄清楚在数组上创建迭代器的方法。

1 个答案:

答案 0 :(得分:0)

不确定这是否是您正在寻找的...这是使用mongoose架构而不是实际的mongodb文档的测试。

将答案标记为正确或让我知道测试/代码是否需要改进。

Mocha Test result

Mocha测试文件

var expect = require('chai').expect,mongooseMock = require('mongoose-
mock'),proxyquire=require('proxyquire'),
sinon = require('sinon'), chai=require('chai'),sinonChai = require("sinon-chai");chai.use(sinonChai);

var AIterator;
describe('model', function() {
        beforeEach(function (done) {
                AIterator = proxyquire('./ArrayIterator', {'mongoose': mongooseMock});                done();   
        });
it("should iterate the array",function(done){
        setTimeout(done, 15000);
        var callback = sinon.spy();
        var aIterator =[];
        aIterator[0]= AIterator.create({"key":"value1","key2":"value2","keyN":"value3"}, callback);
        aIterator[1]= AIterator.create({"key":"value4","key2":"value5","keyN":"value6"}, callback);
        aIterator[2]= AIterator.create({"key":"value7","key2":"value8","keyN":"value9"}, callback);
        var x=0;
         aIterator.forEach(function(element) {
                if (element.keyN=="value9")
                {
                element.keyN="test";        
                }                 
         }, this);
        expect(aIterator[2].keyN).equals("value3");
        done(); 
});
});

代码文件

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var arraySchema = new Schema({
  key:String,
  key2:String,
  keyN:String
});

var NewArray = new mongoose.Schema(arraySchema);
NewArray.statics.create = function (params, callback) {
  var newUpdate = new NewArray(params);
  newUpdate.save(function(err, result) {
    callback(err, result);
  });
  return newUpdate;
};
var ArrayIterator = mongoose.model('Model', NewArray);
module.exports = ArrayIterator;

或者,

另一个测试来运行输入数组中的值,如果这是你的意思:

it("should iterate the array",function(done){
        setTimeout(done, 15000);
        var callback = sinon.spy();
        var testArray=["test","test1"];
        var aIterator =[];
        aIterator[0]= AIterator.create({"key":"value1","key2":"value2","keyN":"value3"}, callback);
        aIterator[1]= AIterator.create({"key":"value4","key2":"value5","keyN":"value6"}, callback);
        aIterator[2]= AIterator.create({"key":"value7","key2":"value8","keyN":"value9"}, callback);
        var x=0;
        testArray.forEach(function(element){
                if (element=="test")
                {
                 aIterator.forEach(function(element1) {
                 if (element1.keyN=="value9")
                 {
                   element1.keyN=element;        
                        }                 
                }, this);
                }
                 },this);
        expect(aIterator[2].keyN).equals("value3");
        done(); 
});