我正在尝试使用mongoose来创建数据库和集合.我的代码是:
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/testdb');
var Schema = mongoose.Schema;
var UserInfo = new Schema({
username : String,
password : String
});
mongoose.model('UserInfo', UserInfo);
var user = db.model('UserInfo');
var admin = new user();
admin.username = "sss";
admin.password = "ee";
admin.save();
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,mongoose创建了名为UserInfo而不是userinfo的集合.如何在mongoose中强制收集名称?
我正在使用mongoose列出mongodb中db中集合的所有数据:
来自要求:
http://localhost:3000/listdoc?model=Organization
Run Code Online (Sandbox Code Playgroud)
我正在做以下代码:
exports.listDoc = function(req, res) {
var Model = mongoose.model(req.query.model); //This is defined and returns my desired model name
Model.find().populate('name').exec(function(err, models) {
if (err) {
res.render('error', {
status: 500
});
} else {
res.jsonp(models);
}
});
};
Run Code Online (Sandbox Code Playgroud)
我已经在数据库中输入了我的条目但上面的代码返回空.为什么?
编辑:以下代码也返回空:
exports.listDoc = function(req, res) {
var Model = mongoose.model(req.query.model);
Model.find({},function(err,models){
console.log(models);
if (err) {
res.render('error', {
status: 500
});
} else {
res.jsonp(models);
}
});
};
Run Code Online (Sandbox Code Playgroud)
使用的架构:
var Organization = mongoose.Schema({
name: String
});
Run Code Online (Sandbox Code Playgroud) 我有两个模型,user 和 schedule,我想使用$lookup和 mongoose将这两个模型结合起来。
用户(型号)
name: {
type: String,
required: true
},
firstName: {
String
},
lastName: {
String
},
storeKey: {
type: String,
required: true
},
avatar: String,
birthday: String,
phone: {
type: String
},
doc: String,
email: {
type: String
},
password: {
passwordHash: String,
salt: String
},
active: {
type: Boolean,
default: true
},
deleted: {
type: Boolean,
default: false
},
generalObservations: {
type: String
},
from: {
type: String
},
services: {
type: Number, …Run Code Online (Sandbox Code Playgroud) mongoose mongodb node.js mongodb-query aggregation-framework
这是我的代码:
var mongoose = require('mongoose');
var db = mongoose.createConnection('localhost', 'mcfly');
db.once('open', function() {
var schema = new mongoose.Schema({
firstname: String,
lastname: String
});
var col = db.model('mathpeep', schema);
col.find({firstname: 'hey'}, function(err, Col) {
console.log(Col);
});
})
Run Code Online (Sandbox Code Playgroud)
我的 mongod 运行正常,我运行了“mango”并在集合“mathpeep”中的数据库“mcfly”中输入了几个条目。这段代码基本上是从它工作的另一个地方复制的,一直到变量名,所以我不知道出了什么问题。
问题是我的数据库看起来像这样:
> use mcfly
switched to db mcfly
> db.createCollection('mathpeep')
{ "ok" : 1 }
> show collections
mathpeep
system.indexes
> db.mathpeep.insert({firstname: 'hey', lastname: 'ayy'})
WriteResult({ "nInserted" : 1 })
> db.mathpeep.insert({firstname: 'aaa', lastname: 'aaaaa'})
WriteResult({ "nInserted" : 1 })
> …Run Code Online (Sandbox Code Playgroud) 我有一个问题,真的可以使用一些帮助。我们有一个使用 MySQL 的应用程序,我们正在切换到 MongoDB。我们将 Node.js 与 Express 一起使用,并使用 MongoDB 和 Mongoose 作为数据库
我们查看了 MongoDB 和 Mongoose 文档并在 Stack Overflow 上搜索了相关问题,但我们似乎遗漏了一些东西。
这就是我们在 app.js 中的内容:
var mongo = require('mongodb');
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost:27017/DBName');
Run Code Online (Sandbox Code Playgroud)
这是我们的 usersModel.js
var db = require('mongoose');
var Schema = db.Schema;
var userSchema = new Schema({
u_id : Number,
u_name : { type: String, required: true },
u_lastname : String
});
module.exports = db.model('user', userSchema);
Run Code Online (Sandbox Code Playgroud)
这就是我们在 userController 中使用的:
var User = require('../models/usersModel');
User.find({}, function(err, users) { …Run Code Online (Sandbox Code Playgroud) 进出口新的nodejs.i已经与集合名称PLN名为AQI分贝。我试着去显示所有在收集网页上的记录,但猫鼬总是返回空数组。我曾与其他DBS测试,但我可以从他们那里得到的数据但PLN猫鼬总是返回空数组。我真的很感激,如果有人可以帮助我。这是我的架构
var Pln = new Schema({
latit : Number,
longit : Number,
timestmp : String,
co : Number,
smoke : Number,
O3 : Number,
humidity : Number,
temperature: Number,
co2 : Number,
dust : String
});
var plnModel = mongoose.model('pln', Pln);
Run Code Online (Sandbox Code Playgroud)
这是我的路线。
app.get('/', function(req, res) {
res.contentType('application/json');
plnModel.find({}, function(err, pln) {
if (pln != null) {
console.log('Found the User:' + pln.latit);
res.send(JSON.stringify(pln));
}
});
});
Run Code Online (Sandbox Code Playgroud)