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 资料有点旧
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 资料有点旧
阅读全文
0 0
- mongodb 简单学习使用
- mongodb学习-搭建环境及简单使用
- MongoDB简单学习
- mongoDB简单使用
- mongodb的简单使用
- mongodb java简单使用
- mongoDB简单使用
- MOngoDB的简单使用
- mongodb使用简单总结
- MongoDB学习之四----文本索引的简单使用
- Mongodb学习日志(一):安装使用及简单操作
- 学习笔记3.14--mongodb的安装和简单的使用
- MongoDB学习使用记
- 安装MongoDB和简单使用
- MongoDb随笔,PyMongo简单使用
- mongodb的group简单使用
- SpringBoot中简单使用mongodb
- MongoDB学习笔记(二) MongoDB基本使用
- C++获取数组的长度
- JavaScript hasOwnProperty() 函数详解
- 二叉树的基本操作
- Highways
- EventUtil原生事件工具
- mongodb 简单学习使用
- 工厂模式
- 正则表达式学习参考
- redis简介
- 一款便携式4G高清激光夜视取证仪
- ZooKeeper 学习 (四) ZooKeeper Java客户端API使用
- note_cloud--笔记分享功能
- Mysterious Bacteria
- 算法竞赛入门经典(第二版)紫书-题目集合【例题+习题】