mongodb 简单学习使用
2017-08-03 22:51
295 查看
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 资料有点旧
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 资料有点旧
相关文章推荐
- Mongodb学习日志(一):安装使用及简单操作
- MongoDB学习之四----文本索引的简单使用
- MongoDB的使用学习之(五)Spring集成MongoDB以及简单的CRUD
- FlashCom学习第二课:创建一个使用视频的简单的Flash动画
- PHP学习使用资源简单总结
- PHP学习使用资源简单总结
- 通过制作一个简单的时钟学习WPF中DispatcherTimer的使用 Level 100
- 通过一个简单的SWING日历BEAN开发学习Calendar类的使用(2)JCalendar源代码
- ORM,ASP.NET中ORM学习,ASP.NET中ORM学习心得,WEB2.0中ORM实现原理,Asp.net简单ORM示例源码详细讲解,Asp.net2.0:如何使用ObjectDataSource(配合ORM )
- 个人学习代码保存:例8.在存储过程中使用简单的事务处理
- 学习NUnit测试2(简单的Nunit使用,用于了解Nunit)
- PHP学习使用资源简单总结
- (学习笔记)简单正则表达式的使用实例[javascript]
- PHP学习使用资源简单总结
- 学习NUnit测试1(简单的Nunit使用,用于了解Nunit)
- FreeBSD使用CVSup升级简单学习
- 学习笔记,VB.NET使用DirectSound9 (1) 简单播放
- SAP ABAP/4学习--学习使用OO在ABAP中.简单入门概念.用个例子来说明
- ORM,ASP.NET中ORM学习,ASP.NET中ORM学习心得,WEB2.0中ORM实现原理,Asp.net简单ORM示例源码详细讲解,Asp.net2.0:如何使用ObjectDataSource(配合ORM )(二)
- EJB中JNDI的逻辑名的使用及部署_{EJB之无状态会话Bean简单应用-学习与实践}续(inber原作)