• MongoDB命令语法小用


    using System;
    
      using System.Collections.Generic;
    
      using System.Linq;
    
      using System.Text;
    
      using MongoDB;
    
      namespace ConsoleApplication34
    
      {
    
      class Program
    
      {
    
      static void Main(string[] args)
    
      {
    
      //Create Database
    
      Mongo mongoDBdataBase = new Mongo();
    
      mongoDBdataBase.Connect();
    
      var dataBaseToWork = mongoDBdataBase.GetDatabase("Bloggers");
    
      //Create Collection
    
      var blogger = dataBaseToWork.GetCollection("blogger");
    
      //Insert Records
    
      var b = new Document();
    
      b["Name"] = "Dhananjay";
    
      b["Country"] = "India";
    
      blogger.Insert(b);
    
      b["Name"] = "G Block";
    
      b["Country"] = "USA";
    
      blogger.Insert(b);
    
      //Fetch Record
    
      var searchBlogger = new Document();
    
      searchBlogger["Name"] = "Dhananjay";
    
      var result = blogger.FindOne(searchBlogger);
    
      Console.WriteLine(result.Get("Country").ToString());
    
      Console.ReadKey(true);
    
      }
    
      }
    
      }
    Microsoft Windows [版本 6.3.9600]
    (c) 2013 Microsoft Corporation。保留所有权利。
    
    
    C:Windowssystem32>e:
    
    E:>cd mongodb
    
    E:MongoDB>cd bin
    
    E:MongoDBin>dir
     驱动器 E 中的卷是 DiskA
     卷的序列号是 0006-89F1
    
     E:MongoDBin 的目录
    
    2014/08/27  11:04    <DIR>          .
    2014/08/27  11:04    <DIR>          ..
    2014/08/08  21:18        10,143,744 mongo.exe
    2014/08/27  11:15                 0 mongod
    2014/08/08  21:28        18,920,960 mongod.exe
    2014/08/08  21:28       129,068,032 mongod.pdb
    2014/08/08  21:29        18,846,720 mongodump.exe
    2014/08/08  21:29        18,796,544 mongoexport.exe
    2014/08/08  21:29        18,825,728 mongoimport.exe
    2014/08/08  21:29        18,866,176 mongorestore.exe
    2014/08/08  21:29        18,836,992 mongostat.exe
    2014/08/08  21:29        18,790,912 mongotop.exe
                  10 个文件    271,095,808 字节
                   2 个目录 63,331,532,800 可用字节
    
    E:MongoDBin>mongo 127.0.0.1:27017
    MongoDB shell version: 2.6.4
    connecting to: 127.0.0.1:27017/test
    >
    > exit
    bye
    
    E:MongoDBin>mongo 127.0.0.1:27017/admin
    MongoDB shell version: 2.6.4
    connecting to: 127.0.0.1:27017/admin
    
    > use foobar
    switched to db foobar
    
    > db.persons.insert({name:"xiangyue"})
    WriteResult({ "nInserted" : 1 })
    > show dbs
    admin   (empty)
    foobar  0.078GB
    local   0.078GB
    zos     0.078GB
    
    > db.system.indexes.find()
    { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "foobar.persons" }
    > db.persons.find())
    { "_id" : ObjectId("53fecb84369886ae9857e137"), "name" : "xiangyue" }
    > db.persons.find()
    { "_id" : ObjectId("53fecb84369886ae9857e137"), "name" : "xiangyue" }
    > db.persons.findone()
    2014-08-28T14:33:47.635+0800 TypeError: Property 'findone' of object foobar.pers
    ons is not a function
    > db.persons.findOne()
    { "_id" : ObjectId("53fecb84369886ae9857e137"), "name" : "xiangyue" }
    
    > db.update.find(name:xiangyue2,{$set:{name:"xiangxiang3"}})
    
    > db.persons.remove({name:"xiangyue2"})
    WriteResult({ "nRemoved" : 1 })
    >

     mongodb与mysql命令对比

    传统的关系数据库一般由数据库(database)、表(table)、记录(record)三个层次概念组成,MongoDB是由数据库(database)、集合(collection)、文档对象(document)三个层次组成。MongoDB对于关系型数据库里的表,但是集合中没有列、行和关系概念,这体现了模式自由的特点。

     

    MySQL

    MongoDB

    说明

    mysqld

    mongod

    服务器守护进程

    mysql

    mongo

    客户端工具

    mysqldump

    mongodump

    逻辑备份工具

    mysql

    mongorestore

    逻辑恢复工具

     

    db.repairDatabase()

    修复数据库

    mysqldump

    mongoexport

    数据导出工具

    source

    mongoimport

    数据导入工具

    grant * privileges on *.* to …

    Db.addUser()

    Db.auth()

    新建用户并权限

    show databases

    show dbs

    显示库列表

    Show tables

    Show collections

    显示表列表

    Show slave status

    Rs.status

    查询主从状态

    Create table users(a int, b int)

    db.createCollection("mycoll", {capped:true,

    size:100000}) 另:可隐式创建表。

    创建表

    Create INDEX idxname ON users(name)

    db.users.ensureIndex({name:1})

    创建索引

    Create INDEX idxname ON users(name,ts DESC)

    db.users.ensureIndex({name:1,ts:-1})

    创建索引

    Insert into users values(1, 1)

    db.users.insert({a:1, b:1})

    插入记录

    Select a, b from users

    db.users.find({},{a:1, b:1})

    查询表

    Select * from users

    db.users.find()

    查询表

    Select * from users where age=33

    db.users.find({age:33})

    条件查询

    Select a, b from users where age=33

    db.users.find({age:33},{a:1, b:1})

    条件查询

    select * from users where age<33

    db.users.find({'age':{$lt:33}})

    条件查询

    select * from users where age>33 and age<=40

    db.users.find({'age':{$gt:33,$lte:40}})

    条件查询

    select * from users where a=1 and b='q'

    db.users.find({a:1,b:'q'})

    条件查询

    select * from users where a=1 or b=2

    db.users.find( { $or : [ { a : 1 } , { b : 2 } ] } )

    条件查询

    select * from users limit 1

    db.users.findOne()

    条件查询

    select * from users where name like "%Joe%"

    db.users.find({name:/Joe/})

    模糊查询

    select * from users where name like "Joe%"

    db.users.find({name:/^Joe/})

    模糊查询

    select count(1) from users

    Db.users.count()

    获取表记录数

    select count(1) from users where age>30

    db.users.find({age: {'$gt': 30}}).count()

    获取表记录数

    select DISTINCT last_name from users

    db.users.distinct('last_name')

    去掉重复值

    select * from users ORDER BY name

    db.users.find().sort({name:-1})

    排序

    select * from users ORDER BY name DESC

    db.users.find().sort({name:-1})

    排序

    EXPLAIN select * from users where z=3

    db.users.find({z:3}).explain()

    获取存储路径

    update users set a=1 where b='q'

    db.users.update({b:'q'}, {$set:{a:1}}, false, true)

    更新记录

    update users set a=a+2 where b='q'

    db.users.update({b:'q'}, {$inc:{a:2}}, false, true)

    更新记录

    delete from users where z="abc"

    db.users.remove({z:'abc'})

    删除记录

     

    db. users.remove()

    删除所有的记录

    drop database IF EXISTS test;

    use test

    db.dropDatabase()

    删除数据库

    drop table IF EXISTS test;

    db.mytable.drop()

    删除表/collection

     

    db.addUser(‘test’, ’test’)

    添加用户

    readOnly-->false

     

    db.addUser(‘test’, ’test’, true)

    添加用户

    readOnly-->true

     

    db.addUser("test","test222")

    更改密码

     

    db.system.users.remove({user:"test"})

    或者db.removeUser('test')

    删除用户

     

     
  • 相关阅读:
    spring + junit 测试
    备份mysql数据库
    JAVA访问Zabbix API
    pro git 读书笔记 3 Git 分支
    pro git 读书笔记 1
    pro git 读书笔记 2
    溢出和剪裁,可见性
    html 与 jsp 文件格式区别
    Eclipse 的 CheckStyle 插件
    【转】单元测试覆盖率
  • 原文地址:https://www.cnblogs.com/cube/p/3941649.html
Copyright © 2020-2023  润新知