使用MongoDB的ORM版Mongose来写你的数据操作吧!

 Adding the MongoDB Backend
The first necessary change is to add a connection to the MongoDB database. It’s added
to the primary app.js file, so the connection persists for the life of the application.
First, Mongoose is included into the file:
var mongoose = require('mongoose');
Implementing a Widget Model with Mongoose | 223
Then the database connection is made:
// MongoDB
mongoose.connect('mongodb://127.0.0.1/WidgetDB');
mongoose.connection.on('open', function() {
console.log('Connected to Mongoose');
});
Notice the URI for the MongoDB. The specific database is passed as the last part of the
URI.
This change and the aforementioned change converting routes to main are all the
changes necessary for app.js.


 //

简单的意思就是说,在你的入口app.js哪里加多下面几行,

这样mongoose就会帮你连接好,后面的所有的操作都不需要再连接了,直接调用结构就好了。

如果你重复连接,就会报警啦!


var mongoose = require('mongoose');

// MongoDB
mongoose.connect('mongodb://127.0.0.1/WidgetDB');
mongoose.connection.on('open', function() {
console.log('Connected to Mongoose');

});


连接好后,我们新建一个 user.js文件,用来存放这个bean(Entity,或者schema也行,随你理解这个类吧)。

var mongoose = require('mongoose');

var Schema = mongoose.Schema
    , ObjectId = Schema.ObjectId;

var User = new Schema({
    _id: {type: Number, require: true, trim: true, unique: true},
    email: {type: String, required: true, trim: true},
    pswd: {type: String, required: true, trim: true}
});
module.exports = mongoose.model('User', User);


然后我们在 userModel.js里面调用他的话,就像下面这样 

var UserBean = require('./Schema/User');

        var user = new UserBean();
        user._id=121;
        user.email=email;
        user.pswd=password;

        console.log("start to find"); 
        //            //注册新用户
         user.save(function (err, data) {         
                  if (err) {
                    console.log("save error="+err); 
                  } else {
                    console.log(data); 
                      }
                    });


ok,是不是很简洁的样子?赶紧在你的项目中用这个吧

详细的介绍,还是看下官方文档吧!:

http://mongoosejs.com/

https://github.com/Automattic/mongoose


其余的nodejs的orm还有

sequelize

https://github.com/sequelize/sequelize

http://sequelizejs.com/


ORM2

https://www.npmjs.com/package/orm




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. MongoDB安装与配置 步骤如下: 1. 下载MongoDB: 在MongoDB官网 https://www.mongodb.com/ 中下载相应的本。 2. 安装MongoDB: 运行下载后的安装程序,按照提示进行安装。 3. 配置MongoDB: 打开MongoDB安装目录下的bin文件夹,找到mongod.exe文件,将其添加到系统环境变量中,然后在命令行中输入mongod,启动MongoDB。 4. 测试MongoDB: 在命令行中输入mongo,连接MongoDB数据库。 2. MongoDB数据操作 MongoDB数据操作主要包括增删改查四个方面,具体操作如下: 1. 插入数据使用insert()方法插入数据,语法如下: db.collection.insert(document) 其中,db.collection是集合名称,document是要插入的数据。 2. 删除数据使用remove()方法删除数据,语法如下: db.collection.remove(query) 其中,db.collection是集合名称,query是删除条件。 3. 更新数据使用update()方法更新数据,语法如下: db.collection.update(query,update,options) 其中,db.collection是集合名称,query是更新条件,update是更新内容,options是更新选项。 4. 查询数据使用find()方法查询数据,语法如下: db.collection.find(query) 其中,db.collection是集合名称,query是查询条件。 3. Java使用MongoDB Java中使用MongoDB需要引入MongoDB驱动程序,然后通过Java代码操作MongoDB数据库。 步骤如下: 1. 引入MongoDB驱动程序:在Java项目中引入MongoDB驱动程序,可以通过Maven或手动下载并添加到项目中。 2. 连接MongoDB使用MongoClient类连接MongoDB数据库,语法如下: MongoClient mongoClient = new MongoClient("localhost", 27017); 其中,localhost是MongoDB数据库所在的主机名,27017是MongoDB数据库所使用的端口号。 3. 获取MongoDB数据库:使用getDatabase()方法获取MongoDB数据库,语法如下: MongoDatabase mongoDatabase = mongoClient.getDatabase("test"); 其中,test是MongoDB数据库的名称。 4. 获取MongoDB集合:使用getCollection()方法获取MongoDB集合,语法如下: MongoCollection<Document> collection = mongoDatabase.getCollection("users"); 其中,users是MongoDB集合的名称。 5. 插入数据使用insertOne()方法插入数据,语法如下: Document document = new Document("name", "Tom").append("age", 18); collection.insertOne(document); 其中,name和age是数据的字段名,"Tom"和18是数据的值。 6. 删除数据使用deleteOne()方法删除数据,语法如下: collection.deleteOne(Filters.eq("name", "Tom")); 其中,name是数据的字段名,"Tom"是数据的值。 7. 更新数据使用updateOne()方法更新数据,语法如下: collection.updateOne(Filters.eq("name", "Tom"), new Document("$set", new Document("age", 20))); 其中,name是数据的字段名,"Tom"是数据的值,$set是更新操作符,age是更新后的值。 8. 查询数据使用find()方法查询数据,语法如下: MongoCursor<Document> cursor = collection.find().iterator(); while (cursor.hasNext()) { Document document = cursor.next(); System.out.println(document); } 其中,find()方法可以不带参数,表示查询所有数据。查询结果会返回一个游标,可以通过循环遍历游标获取查询结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值