我正在使用猫鼬来操作mongodb.现在,为了测试,我想通过本机连接将一些数据插入到mongodb中.
但问题是如何在插入后获取生成的id?
我试过了:
var mongoose = require('mongoose');
mongoose.connect('mongo://localhost/shuzu_test');
var conn = mongoose.connection;
var user = {
a: 'abc'
};
conn.collection('aaa').insert(user);
console.log('User:');
console.log(user);
Run Code Online (Sandbox Code Playgroud)
但它打印:
{ a: 'abc' }
Run Code Online (Sandbox Code Playgroud)
没有_id领域.
我即将构建我的node.js/express/mongoose/passport应用程序,我正在考虑为用户和帐户设置正确的架构.
将有用户从Twitter和Facebook以及本机帐户登录.在稍后阶段,我希望用户将twitter和facebook连接到我的应用程序(可能还有更多的外部帐户).
对于那种情况,我想不出一个好的解决方案.以下是我想到的选项:
1.拥有个人资料模型和帐户模型.配置文件代表唯一用户,而帐户提供用户名和密码(内部帐户)或来自身份验证提供程序(外部帐户)的身份验证数据.配置文件必须至少有一个嵌套的帐户文档.
var ExtAccountSchema = new Schema({
type: String, // eg. twitter, facebook, native
uid: String
});
var IntAccountSchema = new Schema({
username: String,
password: String
});
var ProfileSchema = new Schema({
firstname: String,
lastname: String,
email: String,
accounts: [Account] // Pushing all the accounts in there
});
Run Code Online (Sandbox Code Playgroud)
我不喜欢它的是由不同的帐户数据导致的不一致的帐户文档以及我在用户登录时难以找到合适的帐户(在嵌套文档中搜索uid和帐户类型-.-)的事实
2.将所有数据保存在单个模型中
var ProfileSchema = new Schema({
firstname: String,
lastname: String,
email: String,
twitter-uid: String,
facebook-uid: String
password: String
});
Run Code Online (Sandbox Code Playgroud)
嗯,这只是丑陋 …
我使用Mongoose.js并不能解决3级层次文档的问题.
有两种方法可以做到这一点.
首先 - 没有参考.
C = new Schema({
'title': String,
});
B = new Schema({
'title': String,
'c': [C]
});
A = new Schema({
'title': String,
'b': [B]
});
Run Code Online (Sandbox Code Playgroud)
我需要显示C记录.我怎么能填充/找到它,只知道C的_id?
我试着用:
A.findOne({'b.c._id': req.params.c_id}, function(err, a){
console.log(a);
});
Run Code Online (Sandbox Code Playgroud)
但是我不知道如何从者返回一个只有我需要的对象的对象.
第二,如果使用refs:
C = new Schema({
'title': String,
});
B = new Schema({
'title': String,
'c': [{ type: Schema.Types.ObjectId, ref: 'C' }]
});
A = new Schema({
'title': String,
'b': [{ type: Schema.Types.ObjectId, ref: 'B' }]
});
Run Code Online (Sandbox Code Playgroud)
如何填充所有B,C记录以获得层次结构?
我试着用这样的东西:
A
.find({}) …Run Code Online (Sandbox Code Playgroud) 对于我的项目,我想为组织组保留一份mongoose文档,如下所示:
var groupSchema = Schema({
name : { type : String },
org : { type : Schema.Types.ObjectId, ref : 'Organization' },
...
users : [{
uid : { type : Schema.Types.ObjectId, ref : 'User' },
...
}]
});
Run Code Online (Sandbox Code Playgroud)
我想阻止同一个用户在同一组中两次.为此,我需要强制users.uid在users数组中是唯一的.我尝试为uid说明'unique:true',但这不起作用.有没有办法用mongoose或mongoDB执行此操作而无需额外查询或拆分架构?
编辑:我将uid的先前值更改为uid:{type:Schema.Types.ObjectId,ref:'User',index:{unique:true,dropDups:true}}但是这似乎仍然不起作用.
编辑:假设没有简单的方法来实现这一点,我添加了一个额外的查询检查用户是否已经在组中.在我看来,这是最简单的方法.
我是MongoDB和Mongoose的新手,似乎无法找到关于如何在架构更改时处理迁移的答案.
我习惯于运行迁移SQL脚本来改变表结构和需要更改的任何底层数据.这通常涉及DB停机时间.
这通常是如何在MongoDB/Mongoose中处理的?我需要注意的任何问题?
我的node.js应用程序中出现了一个奇怪的错误
not master and slaveOk=false code 13435
我正在使用mongoose 4.0.2点击mongodb 3.0.1进行findOne查询.该查询今天早些时候正在运行.
这个错误是什么?我正在按照他们的标准每gb计划运行mongohq.
C:\Users\Nick\Desktop\turntablefm\Bots\Super Bot>node bot.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'mongoose'
at Function._resolveFilename (module.js:334:11)
at Function._load (module.js:279:25)
at Module.require (module.js:357:17)
at require (module.js:368:17)
at Object.<anonymous> (C:\Users\Nick\Desktop\turntablefm\Bots\Super Bot\db.j
s:1:78)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Module.require (module.js:357:17)
Run Code Online (Sandbox Code Playgroud)
我已经使用npm install安装它,我尝试重新安装但是没有任何想法吗?
我想在mongodb中为每个集合使用多个模式,如何使用它?
当我尝试运行它时,它给了我这个错误:
错误:
OverwriteModelError:
allUsers编译后无法覆盖模型.
OverwriteModelError:checkInOut编译后无法覆盖模型.
继承我的schema.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
var checkInInfoSchema= new Schema({
name:String,
loginSerialId:Number
});
var loginUserSchema = new Schema({
sn : { type: Number, unique:true }
,uname: {type:String, unique:true}
,pass:String
});
var registerUserSchema = new Schema({
sn : { type: Number, unique:true }
, name: String //his/her name
,pass:String,
companyKey:{type:String},
uname:{type:String,unique:true}
});
var checkInOutSchema = new Schema({
uname: String
,companyKey:String
,task:String
,inTime:String
,outTime:String
,date:{type:String}
,serialId:{type:Number,unique:true} …Run Code Online (Sandbox Code Playgroud) 我从mongoose开始,我想知道如何进行这种配置:

食谱有不同的成分
我有两个型号:
成分和配方:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var IngredientSchema = new Schema({
name: String
});
module.exports = mongoose.model('Ingredient', IngredientSchema);
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var RecipeSchema = new Schema({
name: String
});
module.exports = mongoose.model('Recipe', RecipeSchema);
Run Code Online (Sandbox Code Playgroud) 我试图将star_info属性设置为对象类型(混合模式)并将其默认值设置为空对象使用
star_info: { type : Schema.Types.Mixed, default : { }}
Run Code Online (Sandbox Code Playgroud)
在数据库中,star_info保存文档时没有字段.如何mongoose设置默认值?
mongoose ×10
mongodb ×9
node.js ×5
javascript ×2
nosql ×2
express ×1
mongohq ×1
npm ×1
passport.js ×1