node. JS Manual Query -2-mongodb database method

Source: Internet
Author: User
Tags createindex findone

Mongodb

tags (space delimited): Database MongoDb

Installation
Current version 2.XExtract to any directory, it is best not to beCPlate. Create a folder in the root directory to store the projectMy example: Install to:D:\mongodbCreate a storage directoryD:\mongodb\blogRunCmdCutBinDirectoryCD D:\mongodb\bInch Enable database Mongod -dbpath  "d:\mongodb\blog "  That's it, if you close cmd  set up a fast-start bat because each boot server is such a command   start mongodb. Bat: D:\mongodb\bin< Span class= "PLN" >\mongod.-dbpath d:\mongodb\blog   
Used in node. js
1. package.json dependencies对象中加入 "mongodb": "*"2. 在工程目录下运行 npm install 更新依赖文件3. 引入

var Db = require (' MongoDB '). Db; var Connection = require (' MongoDB '). Connection; var Server = require (' MongoDB '). Server; ' Blog ' database name MongoDB is a db instance var MongoDB = new db (' blog ', New Server (' localhost ', Connection.default_port, {}));

Db API

Properties: Serverconfig topology, such as new Server (' localhost ', Connection.default_port, {}) of the above instance buffermaxentries database current buffer value DatabaseName the current database name, such as the ' blog ' of the above instance

API://Add user to the database db.adduser (username, password, options, callback)//delete user Db.removeuser (username, callback)// Returns the Administrator DB instance db.admin ()//Verify User db.authenticate (username, password, options, callback)//Close connection Force Boolean value, whether forced shutdown db.close ( Force, callback)//Take a specific set db.collection (name, Options, callback)//Get all Collections Db.collections (callback)//Create a collection Db.createcollection (name, Options, callback)//CREATE index Db.createindex (name, Fieldorspec, Options, callback)//delete collection Db.dropcollection (name, callback)//delete database Db.dropdatabase (callback)//Get information from the collection db.listcollections (name, Options, CALLBACK)//Open Database Db.open (callback)//Log Out Database Db.logout (options, callback)//Statistics all data db.stats (options, callback)

General use Process://Open Database Db.open (function (err,db) {//Read collection db.collection (Name,function (err,collection) {//Insert data in collection Collection.insert ({' Age ': +, ' email ': ' xxxx '}, {safe:true}, function (err, user) {Db.close ();})})

})
Collection API

API://query number of matching documents count (query, options, callback)//CREATE INDEX CreateIndex (fieldorspec, Options, callback)//Delete multiple documents Deletemany ( Filter, Options, callback)//delete a document Deleteone (filter, Options, callback)//Delete collection Drop (callback)//delete index in collection dropallindexes (callback)//delete the specified index Dropindex (indexname, Options, callback)//whether there is an index, does not exist to create ensureindex (Fieldorspec, Options, callback )//Query Find (query)//Queries First FindOne (query, Options, callback)//Find and Replace document findandmodify (query, Sort, doc, options, callback) Find and delete findandremove (query, sort, options, callback)//Find a file and delete findoneanddelete (filter, Options, callback)// Locate a file and replace Findoneandreplace (filter, replacement, options, callback)//Find a file and update findoneandupdate (filter, UPDATE, Options, callback)//All index sets Indexes (callback)//check for indexes in the collection indexexists (indexes, callback)//Get index information for this collection Indexinformation (options, callback)//Bulk Write Initializeorderedbulkop (options, callback)

Insert a document into a database in a Docs object or array,Insert(Docs,Options,Callback)InstanceInset({A:1}, {W:1},function(Err,Data){})Insert ArrayInsertmany(Docs,Options,Callback)Insert a single fileInsertone(Doc,Options,Callback)Rebuilding indexesReIndex(Callback)deleting filesRemove(Selector,Options,Callback)Renaming collectionsRename(NewName, Options, Callback) < Span class= "com" >//save save (doc Options, Callback) < Span class= "com" >//statistics all data stats (options Callback) //Update collection  Update (selector, document , Options, Callback)   
Basic documentation
    Multiple keys and values placed together in an orderly manner are documents, basic data unitsJavascript, the document is represented as an object. Each document has a_idThe key, the value is unique within the collection In order: (Below2Documents are completely different) {' Title ':' Xueyou ', ' Age ':+} {' age ':+, ' title ':' xueyou '} syntax:  The key cannot contain the  . Null character, which represents the end of the key  .  and $ have a special meaning, usually keep _ the keys that start with usually also want to keep, although not forcing  MongoDb to differentiate between types also case  -sensitive documents cannot have duplicate keys  
Collection
    看做是表,多个文档组成集合        语法: 不能包含 \0 空字符 不能使空串 "" 不能包含 $ 不能 system 开头.系统保留 system.users存储着数据库内用户的信息 system.namespaces 存储着所有数据库集合的信息
Database
    多个集合组成数据库. 一个MongoDB实例可以承载多个数据库,每个数据库有独立的权限 语法: 不能空串,全部小写,最多64字节,不能特殊字符 因为数据库名称会变成系统的文件 数据库保留名称: admin - local - config
Shell
  mongodb  ;  can run any javascript program,  DOM   start database,   Enter the Span class= "PLN" >bin  run mongo  start shell  current version  2.6. 5  default connection  test  database,   db           

API helo get help exit Shell

Db.Help() View the databaseApidb.Foo.Help() View the collection'sApiGet collectionDb.GetCollection(' Collection name ')Switch to the Foobar database, this time the global variable db is the Foobar databaseSwitched to DB FoobarInserts a document into the collection, DB. Collection name. InsertDb.Blog.InsertObject* * When queryingShellDefault Maximum display20A matching document * *Returns all documents in the collectiondb.  Blog.  Find()//View a document in the collection db.  Blog.  FindOne()//Update document DB/blog.  Update({title:' AA '}, Document Object)//Permanently delete the document from the database, deleting all document DB in the collection without a parameter .  Blog.  Remove();                 
Other
mongod.exe   启动数据库,没参数的时候默认数据目录在 c:\data\dbm使用27017端口, 同时还会启动一个HTTP服务器,监听比端口号大1000的端口28017端口. 访问: http://localhost:28017 可以获取数据库的管理信息

node. JS Manual Query -2-mongodb database method

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.