Mongodb data type and Mongoose common curd "reprint"

Source: Internet
Author: User
Tags jquery library

Objective

After reading the node. JS actual combat, which in the data storage section mentioned Redis, Mongodb, I myself also wrote a few simple demo according to the book introduction, in the demo process first encountered the problem is the data type and common curd. The common operation of MongoDB is in two ways, one is to use the API directly, and it is equivalent to using T-SQL to write SQL statements to manipulate the data in the server client, followed by using the Mongoose driver to manipulate the data in the program. It is equivalent to using ADO or EF to manipulate the data in the program, if you have already written several call API demo, then I suggest to look back at Mongoose API, look at the API, and remember a few common methods such as Where,skip,sort, etc.

In the past, when we wrote the web, we would focus on the techniques of data from the client (views) to the controller layer, then how the data was passed from the controller layer to the DAO layer and the two levels passing the data, so this thought in node is also applicable.

What is MongoDB?

MongoDB is an open source NoSQL database, which is lighter and more flexible than MySQL's relational database, and is ideal for use in situations where data size is large and transactional is not strong.

Mongoose

Mongoose is an object model library that encapsulates the operation of MongoDB and is Nodejs. As if we think native JavaScript difficult to write, the code is much, so with the jquery library, because MongoDB operation Interface complex, inhuman, so there is mongoose. This library is completely optional.
Mongoose is very simple to use, add mongoose to dependence in the app's package.js, then NPM install.

MongoDB data type

1, NULL. {"X": null}.

2, Boolean. {"X": True}, {"X": false}.

3, data type. 64-bit floating-point data is used by default in the MongoDB shell, such as {"X": 2.32}, {"X": 2}, if you want to use an integer type {"x": Numberint (2)}, {"X": Numberlong (2)}.

4, String. The string in MongoDB is encoded in UTF-8, {"x": "Hello World"}.

5. Type of date. {"X": New Date ()}.

6, the regular expression. You can use the same regular expression {"X":/itbilu/i} as JavaScript in MongoDB.

7, data. The use of arrays in MongoDB is the same as JavaScript {"x": ["Hello", "World"]}.

8, embedded documents. {"X": {"Y": "Hello"}}.

9, ID and Objectid (). MongoDB Each document will contain a _id, and MongoDB will automatically generate a Objectid object if you do not specify it.

10, code. {"X": function AA () {}}.)

11, Binary.

Common curd

varMongoose=require ('Mongoose');varSchema=Mongoose. Schema;//1. Connection stringMongoose.connect ('mongodb://localhost/test');//2. Define your data model (that is, the table we define in the relational database)varTodoschema=NewSchema ({title:string, Finished:{type:boolean,default:false}, Post_date:{type:date,default:D Ate.now}});//3. Accessing the Todo object ModelMongoose.model ('Todo', Todoschema);//AddExports.add=function (title,callback) {varnewtodo=NewTodo (); Newtodo.title=title; Newtodo.save (function (err) {if(Err) {Console.log (err);    Callback (ERR); }Else{Callback (NULL); }  });}//finding a separate datavarFindtodobyid=exports.findtodobyid=function (id,callback) {Todo.findone ({_id:id},function (Err,doc) {//Doc is the record value obtained by ID    if(Err) {callback (Err,NULL); } Callback (NULL, Doc); })}//DeleteExports.delete=function (id,callback) {Exports.findtodobyid (Id,function (err,doc) {if(Err) {callback (ERR); }Else{doc.remove (); Callback (NULL); }  })}//Edit Titleexports.edittitle=function (id,title,callback) {Exports.findtodobyid (Id,function (err,doc) {if(Err) {callback (ERR); }Else{doc.post_date=NewDate (); Doc.title=title; Doc.save (function (err) {if(Err) {callback (ERR); }Else{Callback (NULL); }})}exports.alltodos=function (callback) {Todo.find ({},callback);}//Paging QueryExports. todopagelist=function (pageindex,pagesize,callback) {varM=todo.find ({});//There are ways to write this directly: Var m=this;  varStart= (pageindex-1)*pageSize;  M.skip (start);  M.limit (pageSize); M.sort ({'post_date','ASC'});//Sort//paging based on keyword query//m.where (' title ', ' XXX '); //Perform paged queriesm.exec (function (err,rs) {//results after pagination    if(Err) {callback (ERR); }Else{todo.find (function (err,result) {/*paging in general you need to return both the total number of database records and the data after paging, so we use Todo.find to query again*/callback ({rows:rs,total:result.length});    }); }  })}

The above content is small to introduce you to the MongoDB data types and mongoose commonly used curd, I hope you like.

Mongodb data type and Mongoose common curd "reprint"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.