草庐IT

Node-red

全部标签

node.js - Mongoose 条件必填字段验证

根据Mongoosebuilt-invalidatorsdocumentations,我可以使用条件必填字段:constschema=mongoose.Schema({a:{type:String,required:function(){returnthis.b===1}},b:{type:Number,required:true}});在此架构中,仅当属性b等于1时才需要属性a。尝试创建一个新文档按预期工作:Model.create({b:1});//throwsValidationError(propertyaisrequired)和Model.create({b:2});//cr

javascript - 与 Node y Express 约会 Mongodb

我有一个带有node、express和mongodb的api保存functionSaveOfferDay(req,res){varupdate=req.body;varid=req.params.id;Offer.update({"_id":id},{"$addToSet":{"date":{"day":newDate("2018-12-04"),}}},(err,offerUpdated)=>{if(err)returnres.status(500).send({message:'Errorenlapetición'});returnres.status(200).send({off

node.js - MongoDB 递增/递减或更新插入

这个问题在这里已经有了答案:Updatingthepath'x'wouldcreateaconflictat'x'(8个答案)关闭3年前。我正在尝试减少文档中的字段并设置默认值(如果它不存在)。我的查询如下:usersCollection.findOneAndUpdate({_id:id},{$inc:{quota:-1},$setOnInsert:{quota:100}},{upsert:true,returnOriginal:false})但是这个查询给出了以下错误:Updatingthepath'quota'wouldcreateaconflictat'quota'如果我不使用$

node.js - NodeJS 和 ExpressJS 如何抛出错误?

Web应用程序使用express作为服务器,Nodejs作为语言,MongoDB作为数据库,mongoose作为包装器。Express在端口3000上运行服务器,我正在尝试为集合实现基本的CRUD。我使用Newrelic生成图表,我看到当快速抛出错误时响应时间服务器正在上升例如:User.findOne({$or:[{email:{$regex:newRegExp(email,'i')}},{userLogin:{$regex:newRegExp(userLogin,'i')}}]},'id').exec().then(results=>{if(results){thrownewAPI

node.js - 遍历数组并运行 Mongo 查询以修改相同的数组

我有一个集合“用户”,其中包含类似的文档{_id:ObjectId("xx"),searches:[{someId:"yyy","fav_food":"pasta"},{someId:"zzz","fav_food":"macncheese"}]}someId映射到另一个集合“job”{_id:yyy,job_name:"clerk","name":"kent"},{_id:zzz,job_name:"racer","name":"michael"}我必须从工作集合中增强用户集合中的数据所以用户文档应该是:{_id:ObjectId("xx"),searches:[{someId:"c

javascript - 为什么我不能在 Node 中运行此 MongoDB 查询,但它可以在社区指南针中运行?

我正在尝试从我的Node应用程序运行此MongoDB查询,我正在尝试查询特定日期。到目前为止我已经试过了:x=awaitdbo.collection("master").find({Process:"Process1"}).toArray();x=x.map(o=>o.Finish_Time);xtime=recent_time(x)//functionfindsthemaximumtimeinanarrayconsole.log(xtime)//returns'2019-06-12T08:05:33.000Z'query_result=awaitdbo.collection("mas

mongodb - 遍历 node.js 中的数据库记录

我正在学习node.js和mongodb,它们看起来适合我想制作的东西。作为一个帮助我学习的小项目,我想我应该将我拥有的phpbb3论坛中的“posts”表复制到mongodb表中,所以我做了这样的事情,其中​​db是mongodb数据库连接,客户端是mysql数据库连接。db.collection('posts',function(err,data){client.query('select*fromphpbb_posts",function(err,rs){data.insert(rs);});当我在小表上执行时,这工作正常,但我的帖子表中有大约100000行,即使我让它运行一个小

node.js - 比较日期以检索数据

我在mongodb中有这样的数据{"_id":ObjectId("4f0ee7310b09f7a254000001"),"createdAt":ISODate("2012-01-12T23:58:28Z")}{"_id":ObjectId("4f0ee7350b09f7a254000002"),"createdAt":ISODate("2012-01-12T23:58:28Z")}{"_id":ObjectId("4f0ee855e63cecb654000001"),"createdAt":ISODate("2012-01-13T00:03:59Z")}{"_id":ObjectId(

javascript - Node.js + Mongoose ...控制流问题?

刚开始玩node.js,过去有一些JavaScript经验,但在服务器端方面的经验很少。我有以下代码,我正在寻找的功能也是使用Mongoose将产品添加到集合中,但是,如果使用“名称”找到重复项,则将产品作为嵌入式文档推送到重复,只有选择信息,例如产品ID、包信息等。我假设问题与findOne在第一次运行时没有找到任何重复项有关。如果我第二次运行它,它确实会检测到重复项,但到那时为时已晚。我不确定如何让findOne仅在上一次“保存”发生后才运行。functioninsertProduct(data){varproduct;Product.findOne({name:data.name

html - node.js , mongodb, mongoose, html 如何插入数据(在 index.html 中)?

varmongoose=require('mongoose')Schema=mongoose.Schema;mongoose.connect('mongodb://localhost/mydatabase');//connectdatabase/****DefineUserSchema***/varUserSchema=newSchema({user_id:String,email:String,base_location:Number,user_type:String,number_of_event:Number});mongoose.model('User',UserSchema)