本机节点 mongodb 驱动程序 returns 正确的文档,但在由 Mongo-C 创建时具有空的数字数组
Native node mongodb driver returns correct document but with empty Number array when created by Mongo-C
请查看底部的编辑。这似乎是 Mongo-C 和/或本机节点-mongodb 问题,而不是 Mongoose 问题。
我有一个实时收集的地震数据存储在 MongoDB 中。一个称为数据的键是样本的 int 数组。当我通过 Mongoose 查询这个集合时,一切看起来都很好,但是数据键作为一个空数组返回,什么时候应该作为长度为 nsamp(样本数)的数字数组返回。
我已将 Mongoose 架构定义为:
var waveformSchema = new Schema({
key: String,
nsamp: Number,
starttime: Number,
endtime: Number,
samprate: Number,
datatype: String,
data: [Number]
});
通过mongo查询以下记录
db.cwaves.find({"_id" : ObjectId("5733bedeadb31b2b8a0fef2e")})
它returns
{ "_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 ]
}
但如果我在 Mongoose 中执行相同的查询,则数据数组为空:
var mongoose = require('mongoose')
, assert = require('assert')
, app = require('express')()
, http = require('http').Server(app)
, io = require('socket.io')(http);
//schema stuff and things
var Schema = mongoose.Schema;
var waveformSchema = new Schema({
key: String,
nsamp: Number,
starttime: Number,
endtime: Number,
samprate: Number,
datatype: String,
data: [Number]
});
var Cwave = mongoose.model('Cwave', waveformSchema);
//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;
mongoose.connect(url);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
Cwave.find({"_id" : "5733bedeadb31b2b8a0fef2e"}, function(err, cwaves){
if (err) return console.error(err);
console.log(cwaves);
});
});
我得到以下控制台输出:
listening on port: 8888
[ { data: [],
datatype: 'i4',
samprate: 200,
endtime: 1463008989.2350001,
starttime: 1463008988.38,
nsamp: 172,
key: 'BROK.HNZ.UW.--',
_id: 5733bedeadb31b2b8a0fef2e } ]
我是否正确定义了架构?
将脚本1:Changing编辑为:
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 集合是通过使用 Mongo-C.I 创建三个类似文档的 C 程序填充的:1) 通过 mongo-c例程(原始),2) node-mongodb 和 3) Mongo 控制台。当我执行查找时,它们在 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
我使用 mongo gem 创建了一个 ruby 脚本,并且能够读取所有三个数组。
事实证明这是我使用 libbson 构建数组的方式的错误。我的 C 代码中的数组构建部分是:
bson_append_int32 (m_data, "data", -1, long_data[i]);
第二个参数是key,我觉得应该是数组的key"data"。但它应该是作为字符串的索引。
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);
我不明白为什么第一个版本使用 mongo 控制台或 ruby 库工作。
请查看底部的编辑。这似乎是 Mongo-C 和/或本机节点-mongodb 问题,而不是 Mongoose 问题。
我有一个实时收集的地震数据存储在 MongoDB 中。一个称为数据的键是样本的 int 数组。当我通过 Mongoose 查询这个集合时,一切看起来都很好,但是数据键作为一个空数组返回,什么时候应该作为长度为 nsamp(样本数)的数字数组返回。
我已将 Mongoose 架构定义为:
var waveformSchema = new Schema({
key: String,
nsamp: Number,
starttime: Number,
endtime: Number,
samprate: Number,
datatype: String,
data: [Number]
});
通过mongo查询以下记录
db.cwaves.find({"_id" : ObjectId("5733bedeadb31b2b8a0fef2e")})
它returns
{ "_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 ]
}
但如果我在 Mongoose 中执行相同的查询,则数据数组为空:
var mongoose = require('mongoose')
, assert = require('assert')
, app = require('express')()
, http = require('http').Server(app)
, io = require('socket.io')(http);
//schema stuff and things
var Schema = mongoose.Schema;
var waveformSchema = new Schema({
key: String,
nsamp: Number,
starttime: Number,
endtime: Number,
samprate: Number,
datatype: String,
data: [Number]
});
var Cwave = mongoose.model('Cwave', waveformSchema);
//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;
mongoose.connect(url);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
Cwave.find({"_id" : "5733bedeadb31b2b8a0fef2e"}, function(err, cwaves){
if (err) return console.error(err);
console.log(cwaves);
});
});
我得到以下控制台输出:
listening on port: 8888
[ { data: [],
datatype: 'i4',
samprate: 200,
endtime: 1463008989.2350001,
starttime: 1463008988.38,
nsamp: 172,
key: 'BROK.HNZ.UW.--',
_id: 5733bedeadb31b2b8a0fef2e } ]
我是否正确定义了架构?
将脚本1:Changing编辑为:
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 集合是通过使用 Mongo-C.I 创建三个类似文档的 C 程序填充的:1) 通过 mongo-c例程(原始),2) node-mongodb 和 3) Mongo 控制台。当我执行查找时,它们在 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
我使用 mongo gem 创建了一个 ruby 脚本,并且能够读取所有三个数组。
事实证明这是我使用 libbson 构建数组的方式的错误。我的 C 代码中的数组构建部分是:
bson_append_int32 (m_data, "data", -1, long_data[i]);
第二个参数是key,我觉得应该是数组的key"data"。但它应该是作为字符串的索引。
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);
我不明白为什么第一个版本使用 mongo 控制台或 ruby 库工作。