mongoDB基本操作

来源:互联网 发布:js得到当前时间 编辑:程序博客网 时间:2024/06/06 08:29

目录

  • 目录
  • mongodb数据模型
    • mongodb设计模式的一些考虑
  • mongodb基本操作
    • 创建数据库
    • 检查数据库列表
    • 删除一个现有的数据库
    • 创建集合
    • 插入文档
    • 查询文档
    • 更新文档
    • 删除文档
    • 获取索引情况
    • 创建索引

mongodb数据模型

在mongodb中的数据有灵活的模式。在相同集合中文档并不需要有相同的一组字段或结构的公共字段的集合,文档可容纳不同类型的数据

mongodb设计模式的一些考虑

  • 可根据用户要求设计架构
  • 合并对象为一个文件,如果要将他们放在一起。否则,分开它们(但确保不需要连接)
  • 重复数据(有限),因为磁盘空间便宜(相比计算时间)
  • 不需要连接写入,而是读
  • 优化架构是最常见的用例
  • 在模式上做复杂的聚集

mongodb基本操作

创建数据库

use database_name

如:use test

检查数据库列表

show dbs

删除一个现有的数据库

>use testdbswitched to db testdb>db.dropDatabase(){"droped":"testdb","ok":1}

注意:这将删除选定的数据库。如果没有选择任何数据库,会删除默认的test数据库

创建集合

//name是要创建的集合名称,options是一个文件,用于指定配置的集合db.createCollection(name,options)
参数 类型 描述 Name String 要创建的集合名称 Options Document (可选)指定有关内存大小和索引选项

注意:在mongodb中不需要创建集合,当插入一些文件mongodb自动创建集合,

>db.test_collection.insert({"name":"sqliang"})>show collectionstest_collections等等会显示出来

插入文档

>db.test_collection.insert({    //_id:1    name:"sqliang",    age:20,    url:'XXX'})

注意:在插入文档的时候,如果不指定_id参数,mongodb会为文档分配一个独特的ObjectId,另外要插入单个查询的多个文档,可以传递一个insert()命令的文件:

db.post.insert([{   title: 'MongoDB Overview',    description: 'MongoDB is no sql database',   by: 'tutorials yiibai',   url: 'http://www.baidu.com',   tags: ['mongodb', 'database', 'NoSQL'],   likes: 100},{   title: 'NoSQL Database',    description: 'NoSQL database doesn't have tables',   by: 'tutorials yiibai',   url: 'http://www.google.com',   tags: ['mongodb', 'database', 'NoSQL'],   likes: 20,    comments: [        {         user:'user1',         message: 'My first comment',         dateCreated: new Date(2013,11,10,2,35),         like: 0       }   ]}])

查询文档

db.test_collection.find()//显示所有的文件db.test_collection.find().pretty()//结果显示在一个格式化的的方式//相当于where name="sqliang"db.test_collection.find({"name":"sqliang"}).pretty()//相当于where name="sqliang" and age=20db.test_collection.find({"name":"sqliang",age:20}).pretty()//相当于where name="sqliang" or age=20db.test_collection.find({$or:[{"name":"sqliang"},{"age":20}]}).pretty()//相当于where likes>10 and (by="hello" or title="mongodb overview")db.test_collection.find("likes":{$gt:10},{$or:[{"by":"hello"},{"title":"mongodb overview"}]}).pretty()//排序,按照name的正序(1,倒叙为-1)排列结果db.test_collection.find().sort({"name":1}).pretty()//指定返回结果的最大数量db.test_collection.find().limit(5)//跳过前两条数据db.test_collection.find().skip(2)

更新文档

update()方法更新现有的文档值,而替换现有的文档通过文件中的save()方法

//db.test_collection.update({'title':'Mongodb Overview'},{$set:{'title':'sqliang'}})

**注意:**mongoDB默认更新单一的文件,更新多个需要设置参数置“multi“为true

db.test_collection.update({'title':"sqliang"},{$set:{'title':'test'}},{multi:true})

删除文档

remove()用于从集合中删除文档,remove()方法接受两个参数,第一个是删除criteria,第二个是justOne标识
1. deletion criteria:(可选)删除标准,根据文件将被删除
2. justOne:(可选)如果设置为true或1,然后只删除一个文件

//按条件删除db.test_collection.remove({'title':'sqliang'})//删除所有db.test_collection.remove()//多个记录则只删除第一条db.test_collection.remove({'title':'sqliang'},1)

获取索引情况

db.test_collection.getIndexes()

创建索引

db.test_collection.ensureIndex({x:1})

注意:

0 0
原创粉丝点击