MongoDBコマンド構文小用

19862 ワード


 
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:\Windows\system32>e:



E:\>cd mongodb



E:\MongoDB>cd bin



E:\MongoDB\bin>dir

   E   DiskA

   0006-89F1



 E:\MongoDB\bin  



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:\MongoDB\bin>mongo 127.0.0.1:27017

MongoDB shell version: 2.6.4

connecting to: 127.0.0.1:27017/test

>

> exit

bye



E:\MongoDB\bin>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)の3つの階層概念から構成され、MongoDBはデータベース(database)、コレクション(collection)、ドキュメント・オブジェクト(document)の3つの階層から構成されています.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:10000000})また、暗黙的にテーブルを作成します.
テーブルの作成
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')
ユーザーの削除