草庐IT

mongoose-dbref

全部标签

node.js - 类型 'x' 上不存在 Mongoose 属性 'Document'

ts编译器说“文档”类型上不存在任何属性。似乎正因为如此,模型上的所有属性都无法设置。如果我将用户对象打印到控制台,我得到的只是{"_id":"长十六进制ID号"}我的模型配置如下:用户.tsimport*asmongoosefrom'mongoose';import*ascryptofrom'crypto';import*asjwtfrom'jsonwebtoken';varuserSchema=newmongoose.Schema({email:{type:String,unique:true,required:true},firstMidname:{type:String,req

node.js - 一旦保存在 Mongoose 中,如何使某些字段不可更新?

我已经构建了一个架构如下:constUserInfoSchema=newSchema({email:{type:String,required:true,unique:true},username:{type:String,required:true,unique:true},userId:{type:Schema.Types.ObjectId,ref:'User'},displayName:{type:String,required:true},profilePic:{filename:{type:String},url:{type:String}},created_at:Date,

node.js - 如何访问预更新 Mongoose 中间件中的文档对象?

考虑一下,我有一个像这样的Mongoose模式constmySchema=newmongoose.schema({val1:Number,val2:Numberval_1_isBigger:Boolean})现在我想在每次更新操作之前比较val1和val2,因此我想为val_1_isBigger设置bool属性。我的问题是如何在pre('update')mongoose插件操作期间访问文档对象。看下面的例子mySChema.plugin(function(schema,options){schema.pre('update',function(next){//HereHowDoIcom

node.js - Mongo/Mongoose 聚合 - $redact 和 $cond 问题

我很幸运能够得到另一个SO问题的精彩答案Mongo/Mongoose-AggregatingbyDate来自@chridam,它给出了一组文档,例如:{"_id":ObjectId("5907a5850b459d4fdcdf49ac"),"amount":-33.3,"name":"RINGGO","method":"VIS","date":ISODate("2017-04-26T23:00:00Z"),"importDate":ISODate("2017-05-01T21:15:49.581Z"),"category":"NotSet","__v":0}{"_id":ObjectId

javascript - Mongoose 切换多个连接

我正在使用Node.Js8.6和Mongoose4.11,并且有多个数据库连接。数据库连接是通过mongoose.createConnection建立的。我发现mongoose对象具有connections属性(数组),我可以在其中看到已建立的连接。我的问题是,在单独的模块中创建数据库模型时,在连接之间切换的正确方法是什么。索引.jsasyncfunctiondb1(){awaitmongoose.createConnection(process.env.MONGODB_URI_1,{useMongoClient:true});}asyncfunctiondb2(){awaitmong

mongodb - 在 Mongo 中使用 Cursor() 与 Mongoose 聚合

我最近在我的开发环境中更新了mongo以利用新功能(Mongod1.4-->4.0)。现在我所有现有的聚合查询都返回错误:{[MongoError:The'cursor'optionisrequired,exceptforaggregatewiththeexplainargument]name:'MongoError',ok:0,errmsg:'The\'cursor\'optionisrequired,exceptforaggregatewiththeexplainargument',code:9,codeName:'FailedToParse'}我知道我应该能够将“cursor()

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 - 我可以在返回的文档中使用 mongoose 进行查询吗?

我有一个这样的模式...varTerritorySchema=newSchema({user:Schema.Types.ObjectId,streets:[streets_schema])};varStreetsSchema=newSchema({name:String,odd:[block_schema],even:[block_schema],tags:[String])};需要能够通过用户ID找到领土。但是,然后我需要在返回的文档中的街道中按名称找到一条街道。这就是我正在做的...TerritoryModel.findOne({user:userId}).then(territo

mongodb - 如何在寻找嵌入式文档的 Mongoose 中执行更新插入?

SocialProfileSchema=newmongoose.Schemasource:type:Stringenum:['twitter','facebook']lowercase:trueuser_id:Stringprofile_url:Stringprimary:type:Booleandefault:trueadded_on:type:Stringdefault:Math.round(newDate().getTime()/1000.0)UserProfileSchema=newmongoose.SchemasocialProfiles:[SocialProfileSche

javascript - Mongoose 和字段类型 - 数字返回对象,字符串返回字符串?

假设您像这样定义一个Mongoose模式aSchema=newSchemacount:Numbertext:StringA=mongoose.model"A",aSchemadb=mongoose.connect"mongodb://localhost/test"a=newAa.count=99a.text="foo"a.save(err)->A.findByIda,(err,a)->console.logtypeofa.text,typeofa.count#printsstring,objectString类型的字段按预期运行。但是数字字段作为对象返回,这意味着它们需要在用于比较等之