0

请参阅底部的编辑。这似乎是Mongo-C和or本地节点 - mongodb问题,而不是Mongoose问题。本机节点mongodb驱动程序在由Mongo-C创建时返回正确的文档,但是具有空数字数组

我有一个存储在MongoDB中的地震数据的实时采集。一个称为数据的关键字是一个int数组样本。当我通过Mongoose查询这个集合时,一切看起来都很好,但数据键作为一个空数组返回时,应该返回为长度为nsamp(样本数)的Number数组。

我已经定义了猫鼬架构为:

var waveformSchema = new Schema({ 
    key:  String, 
    nsamp:  Number, 
    starttime: Number, 
    endtime: Number, 
    samprate: Number, 
    datatype: String, 
    data:  [Number] 
}); 

通过蒙戈查询以下记录

db.cwaves.find({"_id" : ObjectId("5733bedeadb31b2b8a0fef2e")}) 

它返回

{ "_id" : ObjectId("5733bedeadb31b2b8a0fef2e"), 
"key" : "BROK.HNZ.UW.--", "nsamp" : 172, 
"starttime" : 1463008988.38, "endtime" : 1463008989.2350001, 
"samprate" : 200, "datatype" : "i4", 
"data" : [ 1385, 1384, 1385, 1384, 1381, 1386, 1390, 1381, 1385, 
      1387, 1382, 1387, 1384, 1381, 1385, 1386, 1384, 1382, 
      1383, 1384, 1386, 1384, 1381, 1385, 1386, 1384, 1386, 
      1386, 1385, 1385, 1387, 1382, 1383, 1386, 1381, 1382, 
      1384, 1382, 1383, 1386, 1386, 1382, 1383, 1386, 1384, 
      1385, 1385, 1384, 1387, 1387, 1383, 1383, 1388, 1385, 
      1384, 1387, 1382, 1383, 1385, 1384, 1385, 1384, 1383, 
      1385, 1384, 1383, 1387, 1382, 1386, 1387, 1381, 1386, 
      1386, 1390, 1386, 1383, 1388, 1381, 1384, 1389, 1385, 
      1384, 1386, 1387, 1384, 1382, 1384, 1382, 1378, 1387, 
      1390, 1383, 1385, 1383, 1381, 1384, 1385, 1387, 1380, 
      1379, 1387, 1383, 1384, 1384, 1384, 1385, 1382, 1389, 
      1389, 1381, 1385, 1388, 1387, 1386, 1383, 1386, 1383, 
      1382, 1385, 1382, 1384, 1386, 1383, 1382, 1385, 1386, 
      1386, 1387, 1385, 1382, 1380, 1383, 1387, 1378, 1382, 
      1388, 1383, 1385, 1387, 1385, 1386, 1388, 1386, 1384, 
      1382, 1382, 1385, 1385, 1384, 1378, 1384, 1387, 1383, 
      1383, 1382, 1384, 1384, 1388, 1386, 1380, 1386, 1388, 
      1386, 1383, 1384, 1384, 1383, 1387, 1385, 1384, 1386, 
      1382 ] 
     } 

但如果我这样做,在猫鼬同一查询数据数组为空:

​​

我得到以下控制台输出:

listening on port: 8888 
[ { data: [], 
    datatype: 'i4', 
    samprate: 200, 
    endtime: 1463008989.2350001, 
    starttime: 1463008988.38, 
    nsamp: 172, 
    key: 'BROK.HNZ.UW.--', 
    _id: 5733bedeadb31b2b8a0fef2e } ] 

我是否正确定义模式?

编辑1:更改脚本:

Cwave.find({"_id":"5733bedeadb31b2b8a0fef2e"}).lean().exec(
     function(err, cwaves){ if (err) return console.error(err); 
     console.log(cwaves); }) 

产量:

[ { _id: 5733bedeadb31b2b8a0fef2e, 
    key: 'BROK.HNZ.UW.--', 
    nsamp: 172, 
    starttime: 1463008988.38, 
    endtime: 1463008989.2350001, 
    samprate: 200, 
    datatype: 'i4', 
    data: [ data: 1382 ] } ] 

编辑2:在尝试上述后看来,这可能是本机MongoDB的驱动程序。我改变了我的脚本:

var MongoClient = require('mongodb').MongoClient 
    , assert = require('assert') 
    , app = require('express')() 
    , http = require('http').Server(app) 
    , io = require('socket.io')(http); 

//get configs 
var Conf = require("./config.js"); //config file 
var conf = new Conf(); 
//Connect to Mongo 
var url = "mongodb://" + conf.mongo.user + ":" + conf.mongo.passwd + "@" 
     + conf.mongo.host + ":" + conf.mongo.port + "/" + conf.mongo.dbname 
     + "?authMechanism=" + conf.mongo.authMech + "&authSource=" + conf.mongo.authSource; 



MongoClient.connect(url, function(err, db) { 
    if(err) throw err; 
    var collection = db.collection('cwaves'); 
    collection.find({starttime: 1463008988.38, key: "BROK.HNZ.UW.--"}).toArray(function(err, results) { 
     console.log(results); 
     db.close(); 
     }); 
}); 

,得到了相同的结果编辑1.

[ { _id: 5733bedeadb31b2b8a0fef2e, 
    key: 'BROK.HNZ.UW.--', 
    nsamp: 172, 
    starttime: 1463008988.38, 
    endtime: 1463008989.2350001, 
    samprate: 200, 
    datatype: 'i4', 
    data: [ data: 1382 ] } ] 

编辑3:MongoDB的集合是通过使用蒙戈-CI C程序填充创建了三个类似的文件:1)通过mongo-c例程(原始),2)node-mongodb和3)Mongo Console。它们在mongo控制台中看起来类似,当我进行查找时(除了id和键名),所以我不觉得需要添加输出,除非有人真的需要它。我改变了我的节点脚本如下:

MongoClient.connect(url, function(err, db) { 
    if(err) throw err; 
    var collection = db.collection('cwaves'); 
    collection.find().toArray(function(err, results) { 
     // console.log(result); 
     for(var i=0; i< results.length; i++){ 
      console.log("key: " + results[i].key + " type: " + Object.prototype.toString.call(results[i].data) + " length: " + results[i].data.length); 
     } 
     db.close(); 
     }); 
}); 

其中产量:

key: BROK.HNZ.UW.-- type: [object Array] length: 0 
key: mongo_console type: [object Array] length: 172 
key: node-mongodb type: [object Array] length: 172 

我创建了一个Ruby脚本,使用蒙戈的宝石,并能够读取所有三个数组。

+0

你可以试试这个方法: Cwave.find({ “_ ID”: “5733bedeadb31b2b8a0fef2e”})。瘦肉()EXEC(函数(ERR,cwaves){ 如果(ERR)返回console.error(ERR) ; console.log(cwaves); }); –

+0

我用您的请求更新了我的问题。数据:[数据:1382],这似乎是最后一个因素。 – joncon

回答

0

事实证明,这是我用libbson构建数组时的错误。我的C代码中的数组构建部分是:

bson_append_int32(m_data,“data”,-1,long_data [i]);

第二个参数是键,我认为应该是数组键“数据”。但它应该是一个字符串的索引。

http://api.mongodb.com/libbson/current/bson_append_int32.html

所以阵列建立部分变为:

char index[4]; 


    bson_append_array_begin (m_doc, "data", -1, m_data); 
    for (i = 0; i < trh->nsamp; i++){ 
     snprintf(index, 4, "%d", i); 
     if ((strcmp (trh->datatype, "s2")==0) || (strcmp (trh->datatype, "i2")==0)){ 
     bson_append_int32 (m_data, index, -1, short_data[i]); 
     }else{ 
     bson_append_int32 (m_data, index, -1, long_data[i]); 
     } 
     } 
    bson_append_array_end (m_doc, m_data);        

我不明白为什么第一个版本使用蒙戈控制台或Ruby库工作。

相关问题