1、創(chuàng)建數(shù)據(jù)庫 use yu
2砍的、刪除數(shù)據(jù)庫 db.dropDatabase()
3拭嫁、插入數(shù)據(jù) db.yu.insert({"name":"xxx"})
插入一些文檔時會自動創(chuàng)建集合
4榛泛、創(chuàng)建集合
db.createCollection("mycol",{capped:true,autoIndexId:true,size:6142800,max:10000})
5嚎研、刪除集合
db.mycol1.drop()
6蓖墅、插入文檔 (數(shù)據(jù)結構和json基本一樣)
db.mycol1.insert({
title: 'php教程',
description: 'php 是世界上最好的語言',
by: 'php王者',
url: 'http://www.runoob.com',
tags: ['php', '語言'],
likes: 100
})
7、更新文檔
db.col.update({'title':'php教程'},{$set:{'title':'php從入門到放棄'}})
8临扮、刪除文檔
db.col.remove({'title':'bbbbb'})
9论矾、查詢文檔
key2:value2}).pretty() and條件
db.col.find({
$or:[
{key1:value1, key2:value2}
]
}).pretty() or條件
db.col.find({"likes": {$gt:50}, $or: [{"by": "菜鳥教程"},{"title": "MongoDB 教程"}]}).pretty() and和or聯(lián)合使用
10、操作符
$gt (>) $lt (<) $gte (>=) $lte (<=)
Select * from col where likes > 100;
db.col.find({"likes":{$gt:100}})
db.col.find({likes : {$lt :200, $gt : 100}}) >100小于200
db.col.find({"title" : {$type : 2}})
獲取col集合中title為string的數(shù)據(jù)
limit()
db.col.find().limit(2)
skip()
db.col.find({},{"title":1,_id:0}).limit(1).skip(1)
排序sort()
db.col.find().sort({KEY:1}) 1:升序 -1降序