Tag: 数组

如何知道我的数据库数组中存在哪个数组元素?

我想在mongoDB中写一个查询,在那里我想查找数组中的哪些元素存在于数据库数组中'upvotes'我的查询如下 userupvotesmodel.findOne({mobile_no: "1234567890", upvotes : { $in : ["aa", "bdnvh", "563fa4408d88ea6c13c7abba", "djfufhgj", "5625bd9dbe545d2412d4ae62", "fjjshddhjfn", "djfhudh", "jfhshnffj", "sjdhfkskajdk"] } }, function(err, docs) { if (err) { console.log('Error Finding query results'); console.log(err); res.json({success: 0, message : err}); return next(err); } else { if (docs) { console.log('2 mins', currentdate.getMinutes()); console.log('2 secs', currentdate.getSeconds()); console.log('docs', docs); } } }); 现在对于上面的查询,如果我的数组中有任何单个值存在于数据库中,那么它会发送整个upvotes数组,但我无法找出哪些元素存在,例如数据库中的“aa”,“bdnvh”等我怎么能写一个查询以知道数据库中存在哪些元素。 […]

如何在MongoDB中查询数组中的单个embedded式文档?

我正在试图查询MongoDB中的数组中的单个embedded式文档。 我不知道我做错了什么。 以编程方式,我将查询此文档,并将新的embedded式文档插入到当前空trips数组中。 { "_id" : ObjectId("564b3300953d9d51429163c3"), "agency_key" : "DDOT", "routes" : [ { "route_id" : "6165", "route_type" : "3", "trips" : [ ] }, { "route_id" : "6170", "route_type" : "3", "trips" : [ ] }, … ] } 以下查询 – 我运行在mongo shell – 返回空: db.tm_routes.find( { routes : {$elemMatch: { route_id:6165 } } […]

如何使用variables而不是stringsortingMongoose上的数组索引?

我试图根据variables的索引值对数据进行sorting。 我想有totalClicks[0] ,但totalClicks[0]语法不起作用。 我search并尝试使用'totalClicks.0' ,这工作正常。 但我需要检查req.params.bossId基于array[req.params.bossId]对数据进行sorting。 这是第一个硬编码的代码。 User.find({}).sort({ 'totalClicks.0' : '1'}).exec( function (err, data) { res.json(data); }); 这个dynamicselectsorting的数组字段的代码不工作,整个.sort方法被忽略。 var bossId = req.params.bossId; var totalClicksId = 'totalClicks.' + bossId; console.log(totalClicksId); // totalClicks.0 console.log(typeof(totalClicksId)); // string User.find({}).sort({ totalClicksId : '1'}).exec( function (err, data) { res.json(data); }); 如果IDvariables是0 ,他们我想通过totalClicks[0]sorting数据。 如果IDvariables是1 ,他们我想通过totalClicks[1]sorting数据。 我需要接收一个variablesbossId并使用该variables,基于array[bossId]对数据进行sorting。 我怎么能这样做? 附录: 模型: [ { "username": […]

如何将缓冲区数组转换为hex?

当我调用其中一个API端点时,我得到一个JSON对象中的Buffer数组。 我想这个数组转换为一个更“可用”的forms(hex?),所以我可以比较它们等,这是什么对象目前看起来像: "hash": { "type": "Buffer", "data": [ 151, 14, 51, 26, 46, 52, 5, 151, 99, 107, 38, 188, 138, 180, 76, 56, 108, 214, 135, 213, 125, 134, 105, 139, 129, 236, 206, 157, 67, 1, 12, 12 ] } 我将如何去将这个数组转换为hex(或string等),以便我可以比较散列?

如何使用node.js在mongodb中插入文档数组?

我想使用node.js插入文件数组到mongodb,但插入时只插入第一个数据。 [{ "userid": "5664", "name": "Zero 2679", "number": "1234562679", "status": "contact", "currentUserid": "Abcd" }, { "userid": "5665", "name": "Zero 3649", "number": "1234563649", "status": "contact", "currentUserid": "Xyz" }] 示例代码 collection.insert([{"userid": userid,"name": name,"number": number,"status": status,"currentUserid": currentUserid}], function(err, docs) { if (err) { res.json({error : "database error"}); }else { collection.find({currentUserid:currentUserid}).toArray(function(err, users) { res.send(users); }); }}); 但它仍然插入第一个值,只能告诉我如何插入所有这些文件。 请通过我的post,并build议我一些解决scheme。

设置一个空的数组索引

我有一个数组,可以为给定的问题保存学生的答案。 如果学生给出答案,它会被插入到当前索引的数组中,如answers[questionindex] = answer 后来,我可以读取数组,并映射到给定的问题数组的条目 这个案例: [ "answer", undefined, // student has not given answer "answer2", ] 作品。 (循环数组,只需输出“没有给出答案” if (answers[questionindex] === undefined) 但是当最后的回答没有定义(1个或更多)时,它不起作用(当然)。 我怎么能设置这些领域undefined (如,在计时器达到零后),以显示没有给出答案? 现在,平均计算显示100%正确给予(正确),然后2没有给出 码 var testResults = { addRoom: function(Id, teacher) { // room pseudoconstructor this[Id] = { created: moment(), runningProblem: false, time: 0, // holds the countdown for the current […]

mongoose推嵌套子文档数组

我需要在嵌套子文档数组中推送数据: 这是我的Json: [ { _id: "56cbe9727bab33a413216dd4", company_name: "Star", __v: 0, created_at: "2016-02-23T05:09:06.754Z", plan: [ { plan_name: "Mediclassic", plan_type: "Individual", _id: "56cbe9727bab33a413216dd5", created_at: "2016-02-23T05:09:06.756Z", rate_card: [ { zone: "zone-2", suminsured: 150000, premium: 2874, size: "1A", _id: "56cbe9727bab33a413216dd6", benefits: { hospitals: true, copay: true, cashless_hospital: true, existing_disease_cover: true }, age: { min: "5M", max: 35 } } […]

Node.jsasynchronous数组迭代

我想同步使用async.each遍历数组: async.each(supplier_array, function(supplier) { console.log('looking at : ' + supplier); knex(host_name + '.order_item').where({ supplier: supplier, order_id: order_id }).then(function(data) { console.log(data); knex(host_name + '.distributor').select() .then(function(data) { console.log(data); }).catch(function(error) { console.log('error: ' + error); }); }).catch(function(error) { console.log('error: ' + error); }); }); 我的supplier_array有3个元素。 那么应该发生什么事情呢(同步): 对于供应商1 /第一个ARR /第一个数组元素: 执行console.log(供应商) 执行console.log(order_item) 的console.log(分配器) 对于供应商2 /秒的数组元素: 执行console.log(供应商) 执行console.log(order_item) 的console.log(分配器) 对于供应商3 […]

在mongoose中select几个字段作为数组

如果在我的模式架构数据集我有… var visitSchema = new mongoose.Schema({ title : { type: String}, client : { type: String}, agenda : { type: String}, agm : { type: String }, anchor :{ type: String } )} 所以在这里我想要一些其他的服务,将标题,客户端,议程分成一个数组和agm,锚定到其他数组来检索… 所以我试过这种方式 function getExecsById(id){ var deferred = Q.defer(); console.log("im in getExecsById") var one = ('agm anchor'); //var two =('title client agend'); model […]

我如何dynamic使用for循环,将不同的参数推送到一个数组?

目前我正在尝试使用Node.JS并行运行具有不同参数的相同函数 为此,我使用Async.js,我正在努力尝试推/堆栈函数到一个数组。 问题是这些函数是用相同的参数执行的。 这是我的: var async = require("async"); var array = []; var x = [1,2,3,4]; // This portion of code works perfect and the arguments are passed perfectly // The results that i am getting are: [100, 200, 300, 400] array.push(function(callback){ callback(null,calculate(x[0]))}); array.push(function(callback){ callback(null,calculate(x[1]))}); array.push(function(callback){ callback(null,calculate(x[2]))}); array.push(function(callback){ callback(null,calculate(x[3]))}); // This portion of code does […]