mongodb 简单学习使用

来源:互联网 发布:马云开淘宝网怎么赚钱 编辑:程序博客网 时间:2024/06/03 23:33
1.  service mongodb start/stop/restart
2.  command:
 mongo  connet to mongodb instance
 db.help()
 db.stats()




//database
 show dbs


 use test


 db.movie.insert({"name":"tutorials point"})


use test;
db.dropDatabase();






//collection


use test
db.createCollection("mycollection1")
show collections


db.createCollection("mycol", { capped : true, autoIndexId : true, size : 
   6142800, max : 10000 } )


In MongoDB, you don't need to create collection. MongoDB creates collection automatically, when you insert some document   


use test
db.mycollection1.drop()






//insert to:


db.movie.insert({name:'tutorials point2',
                 title:'title2'
               })


db.movie.insert({ "_id": ObjectId("5982df3f468ac80e8f771b02"),
                 name: 'tutorials point3',
                 title:'title3'
               })  


 
db.movie.insert({name:'tutorials point4',
                 title:'title4'
               })             


ObjectId 长度为24, 否则报错;
使用insert, ObjectId重复时,会报错




_id is 12 bytes hexadecimal number unique for every document in a collection. 12 bytes are divided as follows −
_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 
   3 bytes incrementer)
if we don't specify the _id parameter, then MongoDB assigns a unique ObjectId for this document.


 insert multiple documents: you can pass an array of documents in insert() command
(1)
db.movie.insert([
  {
     title: 'MongoDB Overview', 
     description: 'MongoDB is no sql database',
     by: 'tutorials point',
     url: 'http://www.tutorialspoint.com',
     tags: ['mongodb', 'database', 'NoSQL'],
     likes: 100
  },

  {
     title: 'NoSQL Database', 
     description: 'NoSQL database doesn't have tables',
     by: 'tutorials point',
     url: 'http://www.tutorialspoint.com',
     tags: ['mongodb', 'database', 'NoSQL'],
     likes: 20, 
     comments: [
        {
           user:'user1',
           message: 'My first comment',
           dateCreated: new Date(2013,11,10,2,35),
           like: 0 
        }
     ]
  }
])
(2)
db.movie.save(document)






 db.movie.insert({ "_id": ObjectId("5982df3f468ac80e8f771b02"),
                 name: 'tutorials point5',
                 title:'title5'
               }) 


   
               
 db.movie.save({ "_id": ObjectId("5982df3f468ac80e8f771b02"),
                 name: 'tutorials point5',
                 title:'title5'
               }) 


       db.movie.insert({ "_id": ObjectId("5982df3f468ac80e8f771b03"),
                 name: 'tutorials point6',
                 title:'title6'
               }) 
               
           db.movie.insert({ "_id": ObjectId("5982df3f468ac80e8f771b04"),
                 name: 'tutorials point7',
                 title:'title7',
                 esay: 1
               })  


             db.movie.insert({ "_id": ObjectId("dalldldla"),
                 name: 'tutorials point7',
                 title:'title7',
                 esay: 1
               })                      


     IF _id exists, insert  will have error , but save will success because it will override the orginal document.


(3) the difference between (1) and (2)


If you don't specify _id in the document then save() method will work same as insert() method. If you specify _id then it will replace whole data of document containing _id as specified in save() method








//Query
use test
db.movie.find().pretty()
db.movie.findOne().pretty()


//Query partional fields:
db.movie.find({},{"title":1,_id:0}) 
db.movie.find({},{"name":1,title:1,_id:0})


1 is used to show the field while 0 is used to hide the fields


db.movie.find({},{"name":1,_id:0}).limit(3).skip(1)


//sort 1 asc -1 desc
db.movie.find({},{"name":1,_id:0}).sort({"name": 1})






//update 
update() and save() methods are used to update document into a collection




//delete


db.movie.remove(DELLETION_CRITTERIA, justOne)


db.movie.remove({title:'title7'}, 1)




//index:


db.movie.createIndex({"name": 1}) 






object Id 详解: http://www.cnblogs.com/xjk15082/archive/2011/09/18/2180792.html






mongodb 学习文档:
http://docs.mongoing.com/manual-zh/tutorial/query-documents.html  中文社区
https://github.com/mongodb/mongo-java-driver
https://www.tutorialspoint.com/mongodb/mongodb_java.htm 资料有点旧