Tag: 数组

使用node.js并行操作元素?

我有一个像这样的json对象数组 – var resultsArr = [ { "upvotes": "45", "postID": "4555", "numberOfComments": "45", "shares":"22" }, { "upvotes": "21", "postID": "4665", "numberOfComments": "20", "shares":"24" }, { "upvotes": "4", "postID": "77366", "numberOfComments": "0", "shares":"4" }, { "upvotes": "49", "postID": "6565", "numberOfComments": "22", "shares":"54", }]; 我需要计算基于upvotes , numberOfComments , shares的价值score ,然后将其推回到JSON字典,以便数组中的每个对象看起来像这样 – var resultsArr= [{ …. }, { […]

将MongoDB查询结果转换成JSONArray

我有一个通过mongoose的MongoDB查询,结果是: [ { "tablename": "name1" }, { "tablename": "name2" } ] 但我需要它作为JSONArray: { "tablename": [ "name1", "name2" ] } 有没有简单的方法来转换?

MongoDb从对象属性匹配条件的数组中select对象

我对mongoose和nodejs比较新。 而我试图在nodejs中快速的修改服务器端脚本。 我试图从mongoDb使用mongoose检索数据作为对象的数组。 这是我的模式看起来像 – var heatmapSchema = new Schema({ vuid: String coordinates: [ { x: Number, y: Number, c: Number, timestamp: Number } ] }, {collection: collectionName} ); 正如你所看到的, coordinates是一个对象数组。 我想查询mongoDb,所以我得到这些坐标对象的数组,其中c = 1 ( coordinate c属性等于1),即 – [ { x: 100, y: 230, c: 1, timestamp: 1233312312 }, { x: 120, y: 240, c: […]

蓝鸟承诺的functionarrays

我有一个需要从JSON文件中读取的函数数组: "functionArray" : ["task1()", "task2()", … , "taskN()"] 我的要求是顺序调用这些任务,以便只有在task1函数成功执行后调用task2函数。 将使用这些函数的包装将具有函数定义。 function task1(){ console.log('Inside task1'); } function task2(){ console.log('Inside task2'); } var functionArrayToBeUsed = readFromJson(functionArray); \\functionArrayToBeUsed has all tasks that need to be finished 什么将是一个理想的方式来做到这一点使用承诺。

使用Node JS将Uint8Array转换为Stream

Uint8Array生JavaScript Uint8Array转换为可读或变换Stream的最佳方式是什么? 我想出了如何把Uint8Array的成为缓冲区,不知道这是否有帮助: var uint8array = new Uint8Array(); var buffer = new Buffer(uint8array);

如何在mongodb中更新子文档模式结构中的数组字段

TaskInfo架构: var mongoose = require('mongoose'); var Schema = mongoose.Schema; var taskInfo = mongoose.Schema({ isactive:{type:Boolean}, taskobject:{type:String}, taskdetails:{type:String}, iscompleted:{type:Boolean} }); var TaskInfo = mongoose.model('TaskInfo', taskInfo); module.exports.TaskInfo = TaskInfo; module.exports.TaskInfoSchema = taskInfo; TaskSchema: var mongoose = require('mongoose'); var TaskInfoSchema = require("./taskinfo.js").TaskInfoSchema var Schema = mongoose.Schema; // Task schema var taskSchema = mongoose.Schema({ tasktype : {type: String}, createdon : […]

将新数据推送到外部JSON文件

我想推入一个新的对象到一个外部的JavaScript文件,但我有问题拉JSON文件写入。 我有一个名为check.json的外部本地文件。 如何在节点中正确调用外部json文件? var newData = JSON.parse(check.json); newData.check.push({ cheap: $el.text() }); check.json = JSON.stringify(newData);

用mongoose在不同的对象数组中find一个对象

我必须知道,如果一个项目是在这个模型的第一个或第二个数组中。 Model: _id: String, array1: [{ id: Number, name: String, timestamp: String }], array2: [{ id: Number, name: String, timestamp: String }], array3: [{ id: Number, name: String, timestamp: String }], array4: [{ id: Number, name: String, timestamp: String }] }); 我正在使用这些查询,但任何人都可以帮助我改进它,或者只有一个查询呢? Model.findOne({_id: userId , 'array1.id': item.id },function(error, res) { if(!res){ Model.findOne({_id: userId , 'array2.id': […]

Mongoose创build包含对象数组的logging(Cast to Array失败)

尝试什么似乎是一个相当简单的创build使用对象: var toSave = { person_number: "rjq8900", person_name: "john smith", cars: [{ car_id: "fordTaurus1994", make: "ford", model: "taurus", purchased: "31-Aug-15", price: "1650" }] } 进入模式: var People = new Schema({ person_number: String, person_name: String, cars:[{ car_id: String, make: String, model: String, purchased: Date, price: Number }] }) 通过: People.create(toSave, function(e, doc){ console.log(e); }); 我得到: errors:{ […]

NodeJS&Mongoose:将元素添加到现有的空数组

我正在构build一个NodeJS应用程序,用于根据来自MSQL数据库的数据创build报告。 所有应用程序相关的数据都使用Mongoose存储在MongoDB中。 我的mongoose模型包含一个空的数组,然后由用户通过Rest-API填充。 向数组添加新元素时出现错误。 我已经用model.array.push(object); model.save()试过了model.array.push(object); model.save() model.array.push(object); model.save()和findByIdAndUpdate(…) 。 find我的代码,包括以下两种不同的尝试: mongoose纲要 var sqlSchema = mongoose.Schema({ // Schema to store information about SQL-procedures 'name': String, 'inputs': [ { 'name': String, 'type': String, 'value': String, 'adjustable': Boolean } ] }); REST API 我的应用程序通过POST接受“input”数组的新元素: var mongoose = require('mongoose'), SqlProcedure = mongoose.model('SqlProcedure'); // … router.post('/sql/:id/inputs', function(req, res) { […]