您好,登錄后才能下訂單哦!
這篇文章主要講解了總結(jié)MongoDB數(shù)據(jù)庫的基礎(chǔ)操作,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。
本文實(shí)例講述了MongoDB數(shù)據(jù)庫基礎(chǔ)操作。分享給大家供大家參考,具體如下:
>use test > db.test.insert({"name":1})
>show dbs
> use test > db.dropDatabase()
> db.title.insert({"name":"hyx"})
> show collections
>use test >db.title.drop()
> db.file.insert({name:"huangyuxin",age:11})
>db.files.find()
> document=({by:"hyx"}) { "by" : "hyx" } > db.file.insert(document) WriteResult({ "nInserted" : 1 }) > db.file.find() { "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "huangyuxin", "age" : 11 } { "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "by" : "hyx" } >
> var res = db.file.insertMany([{"b": 3}, {'c': 4}]) > res { "acknowledged" : true, "insertedIds" : [ ObjectId("5c6e8bba0fc535200b893f2b"), ObjectId("5c6e8bba0fc535200b893f2c") ] } > db.file.find() { "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "huangyuxin", "age" : 11 } { "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "by" : "hyx" } { "_id" : ObjectId("5c6e8bba0fc535200b893f2b"), "b" : 3 } { "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 } >
> db.file.update({"name":"huangyuxin"},{$set:{"name":"hyx"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.file.find() { "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "hyx", "age" : 11 } { "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "by" : "hyx" } { "_id" : ObjectId("5c6e8bba0fc535200b893f2b"), "b" : 3 } { "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 } { "_id" : ObjectId("5c6e8cdf0fc535200b893f2d"), "name" : "hyx" } >
> db.file.save({"_id" : ObjectId("5c6e8b1c0fc535200b893f2a"),"name":"hyx"}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.file.find() { "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "hyx", "age" : 11 } { "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "name" : "hyx" } { "_id" : ObjectId("5c6e8bba0fc535200b893f2b"), "b" : 3 } { "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 } { "_id" : ObjectId("5c6e8cdf0fc535200b893f2d"), "name" : "hyx" } >
> db.title.find() { "_id" : ObjectId("5c6e89060fc535200b893f27"), "name" : "yx" } > db.file.find() { "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "hyx", "age" : 11 } { "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "name" : "hyx" } { "_id" : ObjectId("5c6e8bba0fc535200b893f2b"), "b" : 3 } { "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 } { "_id" : ObjectId("5c6e8cdf0fc535200b893f2d"), "name" : "hyx" } > db.file.remove({"b":3}) WriteResult({ "nRemoved" : 1 }) > db.file.find() { "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "hyx", "age" : 11 } { "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "name" : "hyx" } { "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 } { "_id" : ObjectId("5c6e8cdf0fc535200b893f2d"), "name" : "hyx" } >
>db.file.deleteMany({})
>db.file.deleteMany({ status : 1 })
> db.title.find({age:{$gt : 0}}) { "_id" : ObjectId("5c6f7d633ea8783bbfb7fd5e"), "age" : 10 } >
> db.title.find({age:{$gte : 1}})
> db.title.find({age:{$lt:13,$gt:10}}) { "_id" : ObjectId("5c6f7ded3ea8783bbfb7fd5f"), "age" : 12 } { "_id" : ObjectId("5c6f7e833ea8783bbfb7fd60"), "age" : 12 } >
> db.title.find({"name" : {$type : 2}}) { "_id" : ObjectId("5c6e89060fc535200b893f27"), "name" : "yx" } >
> db.title.find().limit(2) { "_id" : ObjectId("5c6e89060fc535200b893f27"), "name" : "yx" } { "_id" : ObjectId("5c6f7d633ea8783bbfb7fd5e"), "age" : 10 } >
> db.title.find({},{"name":1,_id:0}).limit(1) { "name" : "yx" } >
> db.title.find({},{'age':1,_id:0}).sort({age:1}) { } { "age" : 10 } { "age" : 12 } { "age" : 12 } > db.title.find({},{'age':1,_id:0}).sort({age:-1}) { "age" : 12 } { "age" : 12 } { "age" : 10 } { } >
>db.title.createIndex({"age":1})
>db.title.createIndex({"name":1,"age":-1})
>db.col.getIndexes()
>db.col.totalIndexSize()
>db.col.dropIndexes()
>> db.title.dropIndex({'age':1}) { "nIndexesWas" : 2, "ok" : 1 } >
看完上述內(nèi)容,是不是對總結(jié)MongoDB數(shù)據(jù)庫的基礎(chǔ)操作有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。