nodejs mongodb

zyip發表於2014-12-25

 

27017


nodejs指定vsisual studio版本

npm install mongodb --msvs_version=2013

npm install mongoose --msvs_version=2013

 

 

 

db
show dbs

db.getCollectionNames()

use users
db.users.insert({})
show dbs

 

db.users.find()
db.testData.find().limit(3)
db.testData.findOne()


var start = new Date(2010, 3, 1);
var end = new Date(2010, 4, 1);

from datetime import datetime
start = datetime(2010, 4, 1)
end = datetime(2010, 5, 1)

db.users.find({created_on: {$gte: start, $lt: end}});


db.users.insert({name:'zy',created_on:start})

db.users.find({created_on: {$gte: start, $lt: end}});

 

db.collection1.remove({})

 

 

  show users

use admin
db.createUser(
{
user: "admin",
pwd: "123456",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)

 

db.grantRolesToUser(
"admin",
[
{ role: "readWrite", db: "gcal" }
]
)

 

--create user for gmail--

db.createUser({user:'admin',pwd:'123456',roles:[{role:'readWrite',db:'gmail'}]})

 

--install as windows service and tun on the authenticaiton

echo logpath=C:\MongoDB\log\mongo.log > "C:\MongoDB\mongod.cfg"

"C:\MongoDB\bin\mongod.exe" --config "C:\MongoDB\mongod.cfg" --install --auth

 

--  auth=true

 

 

 

------login to mongdb with user and pwd-----------

mongo admin

show users // maybe the db shows an error to warn you need auth

db.auth("admin","adminpwd")

show users  //command return success

 

nodjs to mongodb with auth

mongodb.connect('mongodb://db.abc.com:27017/gcal', function (err, conn) {



            if (!err) {

                conn.authenticate('admin','123456',function(a,b){
                    if(!a){

                        conn.collection('events', function (err, coll) {

 

 

--------------remove all documents from a collection--------------

http://docs.mongodb.org/manual/tutorial/remove-documents/

db.inventory.remove({})

 

 

http://blog.csdn.net/mydeman/article/details/6921723

 

 

 

---manage collections ----

db.createCollections('col1')

show collections

db.col1.drop()

 

相關文章