我已经定义了一个mongoose用户架构:
var userSchema = mongoose.Schema({
email: { type: String, required: true, unique: true},
password: { type: String, required: true},
name: {
first: { type: String, required: true, trim: true},
last: { type: String, required: true, trim: true}
},
phone: Number,
lists: [listSchema],
friends: [mongoose.Types.ObjectId],
accessToken: { type: String } // Used for Remember Me
});
var listSchema = new mongoose.Schema({
name: String,
description: String,
contents: [contentSchema],
created: {type: Date, default:Date.now}
});
var contentSchema = new mongoose.Schema({
name: String,
quantity: String, …Run Code Online (Sandbox Code Playgroud) 我正在学习NodeJs.
要从NodeJS连接并使用MongoDB,我看到很多使用Monk或Mongoose的例子.
这两个库是否相同?它们具有相同的功能还是各自具有特定的用途?
作为NodeJS的初学者,我应该使用哪个?
以下是一些使用Monk的代码示例:
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/nodejsapp');
----
exports.userlist = function(db) {
return function(req, res) {
var collection = db.get('users');
collection.find({},{},function(e,docs){
res.render('userlist', {
"userlist" : docs
});
});
};
};
Run Code Online (Sandbox Code Playgroud)
这里有一个使用Mongoose的示例:
var mongoose = require('mongoose');
----
mongoose.connect('localhost', 'test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback() {
console.log('Connected to DB');
});
// User Schema
var userSchema = mongoose.Schema({
username: { type: String, required: true, unique: true }, …Run Code Online (Sandbox Code Playgroud) 在Mongoose中,我可以使用查询填充来在查询后填充其他字段.我也可以填充多个路径,例如
Person.find({})
.populate('books movie', 'title pages director')
.exec()
Run Code Online (Sandbox Code Playgroud)
然而,这将在书籍上产生查询,收集标题,页面和导演的字段 - 以及查看收集标题,页面和导演字段的电影.我想要的只是从书本中获取标题和页面,从电影中获取导演.我可以这样做:
Person.find({})
.populate('books', 'title pages')
.populate('movie', 'director')
.exec()
Run Code Online (Sandbox Code Playgroud)
这给了我预期的结果和查询.
但有没有办法让第二个片段的行为使用类似"单行"的语法,如第一个片段?原因是,我想以编程方式确定populate函数的参数并将其输入.我不能为多个填充调用执行此操作.
我问这个是因为当我编写单元测试时,我想删除测试数据库并插入一些初始化数据,并在测试中检查mongodb中的数据.所以我需要mongodb的原始操作.
如何在猫鼬中做到这一点?我现在能做的就是创建连接,而不是在mongoose的官方网站上找到任何文件.
var mongoose = require('mongoose');
mongoose.connect('mongo://localhost/shuzu_test');
// get the connection
var conn = mongoose.connection;
Run Code Online (Sandbox Code Playgroud)
但是如何:
我正在使用节点进行 API 并使用 Mongoose。当我yarn dev启动我的 Nodemon 时,Mongo 出现错误,我不知道如何解决这个问题。有人会有什么想法吗?(我正在使用 MongoDB Atlas 数据库)
在以下错误之后。
yarn run v1.22.5
$ nodemon src/server.js
[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node src/server.js`
(node:752) Warning: Accessing non-existent property 'MongoError' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
(node:752) DeprecationWarning: Listening to events on the Db class has been deprecated and will be …Run Code Online (Sandbox Code Playgroud) 我有2个架构,Custphone和Subdomain.Custphone belongs_to一个Subdomain和Subdomain has_many Custphones.
问题在于使用Mongoose创建关系.我的目标是:custphone.subdomain并获取Custphone所属的子域.
我的模式中有这个:
SubdomainSchema = new Schema
name : String
CustphoneSchema = new Schema
phone : String
subdomain : [SubdomainSchema]
Run Code Online (Sandbox Code Playgroud)
当我打印Custphone结果时,我得到了这个:
{ _id: 4e9bc59b01c642bf4a00002d,
subdomain: [] }
Run Code Online (Sandbox Code Playgroud)
当Custphone结果出现{"$oid": "4e9b532b01c642bf4a000003"}在MongoDB中时.
我想做custphone.subdomain并获得custphone的子域对象.
我目前在/models/models.js文件中为我的Mongoose/NodeJS应用程序提供了所有模型(模式定义).
我想将这些分成不同的文件:user_account.js,profile.js等.但是我似乎无法这样做,因为我的控制器断开并报告" 无法找到模块 "一旦我将这些类拆开.
我的项目结构如下:
/MyProject
/controllers
user.js
foo.js
bar.js
// ... etc, etc
/models
models.js
server.js
Run Code Online (Sandbox Code Playgroud)
我的models.js文件的内容如下所示:
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
mongoose.connect('mongodb://localhost/mydb');
var UserAccount = new Schema({
user_name : { type: String, required: true, lowercase: true, trim: true, index: { unique: true } },
password : { type: String, required: true },
date_created : { type: Date, required: true, default: Date.now }
});
var Product = new Schema({
upc : { type: String, …Run Code Online (Sandbox Code Playgroud) 我有Category模特:
Category:
...
articles: [{type:ObjectId, ref:'Article'}]
Run Code Online (Sandbox Code Playgroud)
文章模型包含ref Account model.
Article:
...
account: {type:ObjectId, ref:'Account'}
Run Code Online (Sandbox Code Playgroud)
因此,填充的articlesCategory模型将是:
{ //category
articles: //this field is populated
[ { account: 52386c14fbb3e9ef28000001, // I want this field to be populated
date: Fri Sep 20 2013 00:00:00 GMT+0400 (MSK),
title: 'Article 1' } ],
title: 'Category 1' }
Run Code Online (Sandbox Code Playgroud)
问题是:如何填充填充字段的子字段(帐户)([articles])?我现在就是这样做的:
globals.models.Category
.find
issue : req.params.id
null
sort:
order: 1
.populate("articles") # this populates only article field, article.account is not populated
.exec (err, categories) …Run Code Online (Sandbox Code Playgroud) 我正在学习平均堆栈,当我尝试启动服务器时
npm start
Run Code Online (Sandbox Code Playgroud)
我得到一个例外说:
schema hasn't been registered for model 'Post'. Use mongoose.model(name, schema)
Run Code Online (Sandbox Code Playgroud)
这是我在/models/Posts.js中的代码
var mongoose = require('mongoose');
var PostSchema = new mongoose.Schema({
title: String,
link: String,
upvotes: { type: Number, default: 0 },
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
});
mongoose.model('Post', PostSchema);
Run Code Online (Sandbox Code Playgroud)
正如我所看到的,应该为模型'Post'注册模式,但是什么可能导致异常被抛出?
提前致谢.
编辑:这是异常错误
/home/arash/Documents/projects/personal/flapper-news/node_modules/mongoose/lib/index.js:323
throw new mongoose.Error.MissingSchemaError(name);
^
MissingSchemaError: Schema hasn't been registered for model "Post".
Use mongoose.model(name, schema)
Run Code Online (Sandbox Code Playgroud)
这是使用mongoose初始化的app.js代码:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/news');
require('./models/Posts');
require('./models/Comments');
Run Code Online (Sandbox Code Playgroud)
在行之前:
app.use('/', routes);
Run Code Online (Sandbox Code Playgroud) 已经有一段时间了,因为我正在寻找一个Node.js软件包,以提供一个干净的(但可自定义的)管理界面,以结构化方式对MongoDB数据库执行CRUD操作(允许可信用户编辑涉及自动表单验证的数据,考虑模型之间的关系等).
确实可以使用第三方工具来实现此目的(如Django Admin界面或Rails Admin).
但我想知道是否有一个已经在Node.js上运行的解决方案.
有谁知道这种解决方案?
请注意,我不是在寻找访问数据库的接口,例如phpmyadmin或mongoose-admin.