Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用GridStore存储具有多键Id的文档?_Node.js_Mongodb_Gridfs - Fatal编程技术网

Node.js 如何使用GridStore存储具有多键Id的文档?

Node.js 如何使用GridStore存储具有多键Id的文档?,node.js,mongodb,gridfs,Node.js,Mongodb,Gridfs,我正在使用GridStore,我想存储一个Id为且有多个键的文档 我试过这个: var gridStore = new GridStore(db, {uuid: uuid, format: fileName}, 'w'); // Open the file gridStore.open(function (err, gridStore) { // Write some data to the file gridStore.write(data, function (err, g

我正在使用GridStore,我想存储一个Id为且有多个键的文档

我试过这个:

var gridStore = new GridStore(db, {uuid: uuid, format: fileName}, 'w');

// Open the file
gridStore.open(function (err, gridStore) {
    // Write some data to the file
    gridStore.write(data, function (err, gridStore) {
        assert.equal(null, err);

        // Close (Flushes the data to MongoDB)
        gridStore.close(function (err, result) {
            assert.equal(null, err);

            // Verify that the file exists
            GridStore.exist(db, {uuid: uuid, format: fileName}, 
                function (err, result) {
                    assert.equal(null, err);
                    assert.equal(true, result);
            });
        });
    });
});
但是它失败了,因为
assert.equal(true,result)语句


如何使用GridStore存储具有多键Id的文档?

您应该向GridStore构造函数提供一个文件名,它将起作用:

var gridStore = new GridStore(db, {uuid: uuid, format: fileName}, 'foobar', 'w');
我在GitHub上的GridStore中找到了这段代码(仅显示相关部分):

因此,基本上,如果不提供
filename
选项,GridStore将按文件名引用文档,并为
\u id
字段创建一个新的ObjectId。如果提供文件名选项,
\u id
将是您指定的文件名

您还可以通过检查
fs.files
集合并检查文件的
\u id
值来验证这一点

if(typeof mode === 'undefined') {
  mode = filename;
  filename = undefined;
}

if(id instanceof ObjectID) {
  this.referenceBy = REFERENCE_BY_ID;
  this.fileId = id;
  this.filename = filename;
} else if(typeof filename == 'undefined') {
  this.referenceBy = REFERENCE_BY_FILENAME;
  this.filename = id;
  if (mode.indexOf('w') != null) {
    this.fileId = new ObjectID();
  }
} else {
  this.referenceBy = REFERENCE_BY_ID;
  this.fileId = id;
  this.filename = filename;
}