Tag: bson

Node.js到MongoDB:按date查找

从Nodefinddate到MongoDB的问题: 据说MongoDB可能不是存储date对象,而是一个string,但我不知道如何检查,或者如何解决这个问题。 在我的turnys.js文件中: exports.findNeededTurnys = function(req, handler) { console.log("findNeededTurnys"); var key; //var arg0 = {$or:[{start:{$lte:new Date()}, $where: "this.users.length == this.seats"}]}; var arg0 = {start:{$lte:new Date()}}; console.log("findNeededTurnys: arg0="+JSON.stringify(arg0)); turnydb.collection('turnys', function(err, collection) { collection.find(arg0, {safe:true}, function(err, result) { if(err) console.log("findNeededTurnys: find: err="+err); console.log("findNeededTurnys: find: result="+JSON.stringify(result)); handler.handle(result); }); }); }; 日志文件显示来自MongoDB的空结果? findNeededTurnys: arg0={"start":{"$lte":"2014-03-31T10:17:48.857Z"}} findNeededTurnys: find: result={} 在Mongo中,查询工作(在添加新的Date调用者之后,因为驱动程序可能会在js console.log中将其抽象出来): […]

node.js教程由于缺lessbson模块而失败

背景信息 我试图按照这个教程: http : //cwbuecheler.com/web/tutorials/2014/restful-web-app-node-express-mongodb/ 我有一个mongo数据库在另一个Linux机器上运行… fwiw,我知道这是工作,因为我有一个PHP应用程序,可以连接和查询。 我不使用mongoose,而是和尚,因为这是教程所要求的。 以下是我的package.json文件的样子: { "name": "testapp", "version": "0.0.0", "private": true, "scripts": { "start": "node ./bin/www" }, "dependencies": { "body-parser": "~1.13.2", "cookie-parser": "~1.3.5", "debug": "~2.2.0", "express": "~4.13.1", "mongodb": "^1.4.4", "monk": "^1.0.1", "morgan": "~1.6.1", "pug": "^2.0.0-beta4", "serve-favicon": "~2.3.0" } } 〜 问题 当我尝试运行testing节点应用程序时,出现以下错误消息: me@mydevbox:/var/www/html/node/testapp$ node app.js { Error: Cannot find module […]

将Bson转换为Json对象

Crome开发人员工具显示这一点 console.log('DATA ***',data [0] ._ id); DATA *** Object {_bsontype:“ObjectID”,id:“YIä↵P¨H0”} 我怎样才能将其转换成正常的JSON对象?

在mongodb中通过binDatatypes上传小文件

我想通过BinDatatypes将大小小于16MB的小file upload到MongoDB ,我知道这是小文件的唯一默认选项,而GRIDFS理想地用于超过16MB的文件。 不幸的是,我不能轻易得到正确的文档和上传文件的例子,没有在MongoDB文档上的GridFS。 我发现有关BinDatatypes的信息要么非常有限,要么我不明白。 通过几个类似的问题(主要是基于python的)和其他地方,我对BinData的使用有了一些想法,但仍然无法以这种方式成功上传文件。 我需要通过BinData上传文件的更多信息,特别是正确的方式来初始化,因为我通常得到的BinData not a function或BinData is not defined错误。 以下是我正在testingfunction的当前代码: import { Meteor } from "meteor/meteor"; import { Mongo } from "meteor/mongo"; export const Attachment = new Mongo.Collection("attachment"); let BinData = Mongo.BinData; //wrong initialisation function createAttachment(fileData) { const data = new Buffer(fileData, "base64"); Attachment.insert({file: new BinData(0, data)}); } 一些有用的链接: 蒙大的BSONtypes BSON规格

将项目添加到来自Mongoose查询的BSON文档中的数组时出现意外的行为

从mongoose得到一个结果后,我想添加更多的东西,所以我添加一个_ext字段,这是我从查询得到的扩展副本: […] var schema = new mongoose.Schema( { _id: Number, member_of: {type: [Number], required: true } }); var User = mongoose.model('users', schema); User.findById(1, function(err, result){ console.log(result); 输出: {_id:1,member_of:[1],__v:0} var user = result.toObject(); delete user.__v; user._ext = {}; user._ext._id = user._id; user._ext.name = ''; user._ext.displayName = ''; user._ext.imageUrl = ''; user._ext.member_of = []; for (var i […]

如何使用sailsjs在MongoDb中存储和检索图像?

如何为以下内容定义存储图像的模型 var Person = { attributes: { firstName: 'STRING', lastName: 'STRING', age: 'INTEGER', image: 'STRING' } }; module.exports = Person; 应该是“图像”types和如何存储图像? 如何使用模型'Person'存储和更新图像?

如何将JSON数组保存到mongodb集合中

我试图将下面的数组保存到我的文档中的mongodb {"Category_Id":"54c9c206e1f512456ada778b","Sub_category_Id":"54c9c24ee1f512456ada778d", "features":[{"specification_field_type":"textbox","specification_name":"color","_id":"551e5f2e3bbe4691142bdeba","__v":0,"$$hashKey":"object:95"}, {"specification_field_type":"textbox","specification_name":"Call Features","_id":"551e616d3bbe4691142bdebf","__v":0,"$$hashKey":"object:97"}]} 这里是我的node.js代码保存mongodb中的后期值 var Category_Id = req.body.Category_Id; var Sub_category_Id = req.body.Sub_category_Id; var features = req.body.features; var property =new db.properties(); property.Category_Id = Category_Id; property.Sub_category_Id = Sub_category_Id; property.features = features; property.save(function (err){ var message = JSON.parse('{"status":"success","message":"Features added"}'); return res.send(200, message); } 尽pipe查询被执行,但是特征信息以这种方式保存 集合模式是 var properties = new Schema({ Category_Id : { type: String}, Sub_category_Id […]

如何从包含ISODate的BSONstring中创build一个JSON对象

我想从包含ISODate的string创build一个JSON对象。 var teststring = '{ "_id" : "test001", "CreatedDate" : ISODate("2013-04-02T10:37:21.529Z") }'; console.log(JSON.parse(teststring)); 获取错误: undefined:1 { "_id" : "test001", "CreatedDate" : ISODate("2013-04-02T10:37:21.529Z") } ^

NodeJS中的BSON对象的console.log

我很困惑的方式console.log显示NodeJS的本地MongoDB驱动程序的ObjectId()对象。 我用console.log从MongoDB打印adslot文档: db.collection('adslots').findOne({_id: adslotId}, (err, adslot)=>{ console.log( adslot ); } 和输出是 adslot: { _id: 57ef0b9b26d1d77b606bf271, name: 'cspop', width: 1, height: 1, elemId: 'dummy', active: true, updated: 2016-10-01T01:04:27.597Z } _id看起来像一个hex数字。 但是,_id是ObjectId,因为: console.log( "adslot:\n" + adslot._id.constructor.name ); 给 adslot: ObjectID 尽pipeadslot具有调用isValid() ( http://mongodb.github.io/node-mongodb native / 2.2 / api / ObjectID.html#.isValid)的ObjectId构造函数,但它给出了一个错误: console.log('adslot:'); console.log( adslot._id.isValid() ); 结果: adslot: /home/vlad/arbsrv/node_modules/mongodb/lib/utils.js:98 […]

nodejs + mongoose – 获取文档大小而不查询其所有内容?

在更新或添加数据之前是否可以获取文档大小? 我正在使用这个包来计算文档大小: // Get BSON parser class. // https://www.npmjs.com/package/bson var bson = require("bson"); // Create a bson parser instance. var BSON = new bson(); 这是我的示例代码: var docSize = 0; Piece.findOne({ _id: lastPiece._id }, function(err, piece) { // Get the document size. docSize = BSON.calculateObjectSize(piece); // do something. }) .select("-data"); 但我得到的docSize是不正确的,因为我从查询中挑出 data 。 data是占用大量文档大小的字段,而且我不想查询它,因为在某些情况下,我不需要它,就像上面这样。 那么无论如何不知道文档的内容就知道文档大小呢?