Javascript “寻找猫鼬”;“你好,世界”;例子
更新:已经有一段时间了。但当时决定不使用猫鼬。主要原因是,在使用mongo和javascript时,我们无法找到使用ORM的好理由Javascript “寻找猫鼬”;“你好,世界”;例子,javascript,mongodb,mongoose,Javascript,Mongodb,Mongoose,更新:已经有一段时间了。但当时决定不使用猫鼬。主要原因是,在使用mongo和javascript时,我们无法找到使用ORM的好理由 我一直在尝试用Mongoose创建一个数据库/模型,它基本上只是一个用户名唯一的用户数据库。听起来很简单,但由于某种原因,我一直无法做到这一点 到目前为止,我得到的是: var mongoose = require('mongoose').Mongoose, db = mongoose.connect('mongodb://localhost/db');
我一直在尝试用Mongoose创建一个数据库/模型,它基本上只是一个用户名唯一的用户数据库。听起来很简单,但由于某种原因,我一直无法做到这一点 到目前为止,我得到的是:
var mongoose = require('mongoose').Mongoose,
db = mongoose.connect('mongodb://localhost/db');
mongoose.model('User', {
properties: [
'name',
'age'
],
cast: {
name: String,
age: Number
},
//indexes: [[{name:1}, {unique:true}]],
indexes: [
'name'
]
/*,
setters: {},
getters: {},
methods: {}
*/
});
var User = db.model('User');
var u = new User();
u.name = 'Foo';
u.save(function() {
User.find().all(function(arr) {
console.log(arr);
console.log('length='+arr.length);
});
});
/*User.remove({}, function() {});*/
它就是不起作用。数据库创建正常,但用户名不唯一。是否有任何关于我做错了什么的帮助或知识?尝试在var mongoose=require('mongoose')中给出正确的路径。mongoose 。这对我很管用 # 我的代码
require.paths.unshift("/home/LearnBoost-mongoose-45a591d/mongoose");
var mongoose = require('mongoose').Mongoose;
var db = mongoose.connect('mongodb://localhost/db');
mongoose.model('User', {
properties: ['first name', 'last name', 'age', 'marriage_status', 'details', 'remark'],
});
var User = db.model('User');
var record = new User();
record.first name = 'xxx';
record.last name = 'xxx';
record.age = 'xxx';
record.marriage_status = 'xxx';
record.details = 'xxx';
record.remarks = 'xxx';
record.save(function() {
User.find().all(function(arr) {
console.log(arr);
console.log('length='+arr.length);
});
});
//User.remove({}, function() {});
使用node filename.js编译它
祝你好运。在第一次运行应用程序之前,你应该定义你的唯一索引。否则,您需要放弃收藏,重新开始。另外,当“user1”已经存在时,当您尝试保存{name:'user1}时,mongoose不会抛出错误。您需要定义架构。试试这个:( 对于猫鼬2.7(在节点v.0.8中测试):
var mongoose=require('mongoose'),
Schema=mongoose.Schema;
var db=mongoose.connect('mongodb://localhost/db');
var User=newschema({
名字:String,
姓氏:String
});
var UserModel=mongoose.model('User',User);
var record=newusermodel();
record.first_name='hello';
record.last_name='world';
记录。保存(功能(错误){
查找({},函数(err,users){
for(var i=0,counter=users.length;i\u id:+User.\u id+”,first\u name:+User.first\u name+”,last\u name:+User.last\u name);
}
});
});
Learnboost最近上传了一组示例我知道这个问题已经有10年历史了,原始海报放弃了Mongoose,但由于它出现在谷歌搜索的顶部,我觉得我会提供一个新的答案
提供了一个完整的基本示例,使用Typescript
代码,如适用
异步函数mongooseHelloWorld(){
常量url=mongodb://localhost/helloworld';
//提供避免大量弃用警告的选项
//详情见:https://mongoosejs.com/docs/connections.html
常量选项={
“useNewUrlParser”:true,
“useCreateIndex”:true,
“UseFindModify”:false,
“useUnifiedTopology”:正确
};
//连接到数据库
log(`Connecting to the database at${url}`);
等待mongoose.connect(url、选项);
//创建一个模式,指定字段和
//指示应管理createdAt/updatedAt字段
const userSchema=new mongoose.Schema({
姓名:{
类型:字符串,
必填项:true
},
电邮:{
类型:字符串,
必填项:true
}
}, {
时间戳:真
});
//这将使用主连接。如果需要使用自定义
//连接请参见:https://mongoosejs.com/docs/models.html
const User=mongoose.model('User',userSchema);
//创建两个用户(在多次运行时不会是唯一的)
log(“创建一些用户”);
等待用户。创建({名称:'Jane Doe',电子邮件:'Jane。done@example.abcdef' });
等待用户。创建({名称:'Joe Bloggs',电子邮件:'jane。done@example.abcdef' });
//查找数据库中的所有用户,不带任何查询条件
const entries=wait User.find();
for(设i=0;i注意,这是通过运行Mongoose 4.0.13的Mongoose 5.9.26验证的。为什么会被否决?用户将标准Javascript对象传递到Mongoose.model,而不是模式实例
var mongoose = require('mongoose').Mongoose,
db = mongoose.connect('mongodb://localhost/db'),
Schema = mongoose.Schema;
mongoose.model('User', new Schema({
properties: [
'name',
'age'
],
[...]
}));
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var db = mongoose.connect('mongodb://localhost/db');
var User = new Schema({
first_name: String,
last_name: String
});
var UserModel = mongoose.model('User', User);
var record = new UserModel();
record.first_name = 'hello';
record.last_name = 'world';
record.save(function (err) {
UserModel.find({}, function(err, users) {
for (var i=0, counter=users.length; i < counter; i++) {
var user = users[i];
console.log( "User => _id: " + user._id + ", first_name: " + user.first_name + ", last_name: " + user.last_name );
}
});
});