使用插入數(shù)據(jù)命令:
>insert
需要注意的是,如果插入一文件(現(xiàn)在理解,nosql 類型的db數(shù)據(jù)不能說是記錄了,它是已文件作為單位,而傳統(tǒng)型的關(guān)系型數(shù)據(jù)庫,我們說是插入一條記錄),如果沒有帶人_id,那么會自動生成一個唯一的id,這個id好比關(guān)系型數(shù)據(jù)庫里的主鍵一樣。
如果是自己指定id,那么必須id是唯一的,這點關(guān)系型和nosql型都必須要求的;
數(shù)據(jù)字段的名字不能包含$ 和.
在敲了一陣發(fā)現(xiàn),擦,我要是再shell里面去編寫復(fù)雜的插入語句,就shell的這種一行一句,不搞死我去啊。我想它肯定是支持向bat這種批處理或者其他,看它語法和js一模一樣,于是大膽猜測它使用的就是javascript,于是我就用notepad++來編輯這些插入命令,試著插入復(fù)雜的對象;
哦,另外說一句,如果這個bios數(shù)據(jù)不存在,那么要先使用
>use bios
生成一個數(shù)據(jù)庫,如果你不往這里插入一筆數(shù)據(jù),mongoDB會自動刪除掉這個數(shù)據(jù)庫;
所以還要插入一筆讓mongodb維持這數(shù)據(jù)庫的任意數(shù)據(jù)
>db.bios.insert({a:'a'})
編寫了一個insert.js腳步,放在安裝目錄的js文件夾下。于是這樣輸入:
>mongolocalhost:27017/bios f:\mongodb\js\insert.js
效果如圖
可以看到,這個js腳步成功執(zhí)行;
要查詢這個文檔,輸入命令:
>db.bios.find({ name: { first: 'John', last: 'chen' } } );
有了js寫更多的語句,就方便多了。
Isert語句支持多個批量插入就像下面這樣:
Insert.js 語句:
db.bios.insert(
[
{
_id: 3,
name: { first: 'Grace', last: 'Hopper' },
title: 'Rear Admiral',
birth: new Date('Dec 09, 1906'),
death: new Date('Jan 01, 1992'),
contribs: [ 'UNIVAC', 'compiler', 'FLOW-MATIC', 'COBOL' ],
awards: [
{
award: 'Computer Sciences Man of the Year',
year: 1969,
by: 'Data Processing Management Association'
},
{
award: 'Distinguished Fellow',
year: 1973,
by: ' British Computer Society'
},
{
award: 'W. W. McDowell Award',
year: 1976,
by: 'IEEE Computer Society'
},
{
award: 'National Medal of Technology',
year: 1991,
by: 'United States'
}
]
},
{
_id: 4,
name: { first: 'Kristen', last: 'Nygaard' },
birth: new Date('Aug 27, 1926'),
death: new Date('Aug 10, 2002'),
contribs: [ 'OOP', 'Simula' ],
awards: [
{
award: 'Rosing Prize',
year: 1999,
by: 'Norwegian Data Association'
},
{
award: 'Turing Award',
year: 2001,
by: 'ACM'
},
{
award: 'IEEE John von Neumann Medal',
year: 2001,
by: 'IEEE'
}
]
},
{
_id: 5,
name: { first: 'Ole-Johan', last: 'Dahl' },
birth: new Date('Oct 12, 1931'),
death: new Date('Jun 29, 2002'),
contribs: [ 'OOP', 'Simula' ],
awards: [
{
award: 'Rosing Prize',
year: 1999,
by: 'Norwegian Data Association'
},
{
award: 'Turing Award',
year: 2001,
by: 'ACM'
},
{
award: 'IEEE John von Neumann Medal',
year: 2001,
by: 'IEEE'
}
]
}
]
);
db.bios.insert(
{
name: { first: 'John', last: 'McCarthy' },
birth: new Date('Sep 04, 1927'),
death: new Date('Dec 24, 2011'),
contribs: [ 'Lisp', 'Artificial Intelligence', 'ALGOL' ],
awards: [
{
award: 'Turing Award',
year: 1971,
by: 'ACM'
},
{
award: 'Kyoto Prize',
year: 1988,
by: 'Inamori Foundation'
},
{
award: 'National Medal of Science',
year: 1990,
by: 'National Science Foundation'
}
]
}
)
c = db.bios.find( { name: { first: 'John', last: 'McCarthy' } } );
while((c.hasNext())) printjson(c.next())
=================================================
Mongo還有個save方法,這個方法同樣也是插入數(shù)據(jù)。咋一樣是一樣的,仔細想想如果是一樣的,那干嘛不統(tǒng)一,于是想到了ORM模式中,數(shù)據(jù)的save方法中,如果要保存的數(shù)據(jù)已存在,那么它知道是修改這個數(shù)據(jù),如果不存在則是新增。同樣,這里save也是如此。
如果save方法帶了_id那么,如果數(shù)據(jù)庫中存在了此_id的文檔,那么,會修改原有數(shù)據(jù),如果沒有,則新增,所以,不帶_id的save方法和insert是一樣的,但是Insert方法如果插入的是相同的Id,那么是不會插入的;
更新操作,傳統(tǒng)的數(shù)據(jù)庫更新操作,必須是更新的數(shù)據(jù)已經(jīng)存在,要不然你想,你數(shù)據(jù)記錄都不在便要更新,這不扯淡嗎,但是,mongodb卻可以這樣,如果要更新的數(shù)據(jù)不存在,那么便插入這文檔數(shù)據(jù);
更新語法:
db.collection.update(query>,update>, { upsert: true } )
如果是更新具體某個,那么帶_id的save方法同樣可以完成更新任務(wù);
查詢可發(fā)現(xiàn)改變的數(shù)據(jù):
>c =db.bios.find( { name: { first: 'update', last: 'update' } } );
>while((c.hasNext()))printjson(c.next())
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
您可能感興趣的文章:- Python實現(xiàn)讀取SQLServer數(shù)據(jù)并插入到MongoDB數(shù)據(jù)庫的方法示例
- Python實現(xiàn)批量讀取圖片并存入mongodb數(shù)據(jù)庫的方法示例
- mongoDB 實現(xiàn)主從讀寫分離實現(xiàn)的實例代碼
- python讀取json文件并將數(shù)據(jù)插入到mongodb的方法
- 了不起的node.js讀書筆記之mongodb數(shù)據(jù)庫交互
- mongodb與sql關(guān)系型數(shù)據(jù)比較
- mongodb使用c#驅(qū)動數(shù)據(jù)插入demo
- c#操作mongodb插入數(shù)據(jù)效率
- mongodb實現(xiàn)數(shù)組對象求和方法實例
- Mongodb讀數(shù)據(jù)操作