Tag Archives: mongod

Конспект лекций M102 MongoDB for DBAs (Week 2)

Insert Data

> db..insert()
> db.getLastError()

Более читаемый вид

> db.products.find().pretty()
> db.products.find().toArray()

Добавим данные в таблицу

> x = { name : "Bluetooth Headset 509",
type : ["accessory", "headset"],
price : 30
}
{
        "name" : "Bluetooth Headset 509",
        "type" : [
                "accessory",
                "headset"
        ],
        "price" : 30
}
> db.products.insert( x )
> db.getLastError()
null
db.getLastErrorObj()
{ "n" : 0, "connectionId" : 20, "err" : null, "ok" : 1 }
> db.products.insert({x:3,y:4 })

Updating Documents

> db..update( criteria, objNew, upsert, multi )
> db.getLastError()

criteria – query which selects the record to update;
objNew – updated object or $ operators (e.g., $inc) which manipulate the object
upsert – if this should be an “upsert” operation; that is, if the record(s) do not exist, insert one. Upsert only inserts a single document.
multi – indicates if all documents matching criteria should be updated rather than just one. Can be useful with the $ operators below.

Изменим цену _id : ObjectId(“509115dd3dc1ec0835d21c05”)
> db.products.find({_id : ObjectId(“509115dd3dc1ec0835d21c05”)})
{ “_id” : ObjectId(“509115dd3dc1ec0835d21c05”), “name” : “Bluetooth Headset 509”, “type” : [ “accessory”, “headset” ], “price” : 35 }

> db.products.update({_id : ObjectId(“509115dd3dc1ec0835d21c05”)},{name : “Bluetooth Headset 509”, type : [ “accessory”, “headset” ], price : 40 })

> db.products.find({_id : ObjectId(“509115dd3dc1ec0835d21c05”)})
{ “_id” : ObjectId(“509115dd3dc1ec0835d21c05”), “name” : “Bluetooth Headset 509”, “type” : [ “accessory”, “headset” ], “price” : 40 }

Добавим тестовую запись

> x ={_id : 101 , name : "TEST", available: true}
> db.products.save(x)
> db.products.find({_id: 101})
{ "_id" : 101, "name" : "TEST", "available" : true }

$set

{ $set : { field : value } }
sets field to value. All datatypes are supported with $set.
> db.products.update( {_id: 101}, { $set :{ "available" : false } } )
> db.products.find({_id: 101})
{ "_id" : 101, "name" : "TEST", "available" : false }

$push

{ $push : { field : value } }
Appends value to field, if field is an existing array, 
otherwise sets field to the array [value] if field is not present. 
If field is present but is not an array, an error condition is raised.
> db.products.update( {_id: 101}, {$push :{"cost" : 500}})
> db.products.find({_id: 101})
{ "_id" : 101, "available" : false, "cost" : [ 500 ], "name" : "TEST" }
> db.products.update( {_id: 101}, {$push :{"cost" : 500}})

Removing

> db.things.remove({});    // removes all
> db.things.remove({n:1}); // removes all where n == 1
> db.products.find({_id : 100})
{ "_id" : 100, "available" : 2, "color" : "red", "name" : "GTO", "year" : 1969 }
> db.products.find({_id : 100}).count()
1
> db.products.remove( { _id : 100 } )

Обновления конкретного поля

> db.products.find({_id : "Jane"})
{ "_id" : "Jane", "likes" : [ "tennis", "golf" ], "registered" : false, 
"addr" : { "city" : "Lyon", "country" : "France" } }
> db.users.update({_id:"Jane"},{$addToSet:{likes:"football"}}, true)
> db.products.find({_id : "Jane"})
{ "_id" : "Jane", "likes" : [ "tennis", "golf", "football" ], "registered" : false, 
"addr" : { "city" : "Lyon", "country" : "France" } }

Command Reference

> db.runCommand( {isMaster :1})
{
        "ismaster" : true,
        "maxBsonObjectSize" : 16777216,
        "localTime" : ISODate("2012-11-01T21:32:35.271Z"),
        "ok" : 1
}

Что сейчас происходит с сервером

> db.currentOp()
{ "inprog" : [ ] }

ServerStatus

> db.serverStatus()

Просмотреть Index

> db.products.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "ns" : "pcat.products",
                "name" : "_id_"
        }
]

Добавить Index

> db.products.ensureIndex( {name : 1 })

Удалить Index

> db.products.dropIndex( {name : 1})
{ "nIndexesWas" : 2, "ok" : 1 }
> db.products.find( {for : "ac3"} ).explain()
{
        "cursor" : "BtreeCursor for_1",
        "isMultiKey" : true,
        "n" : 4,
        "nscannedObjects" : 4,
        "nscanned" : 4,
        "nscannedObjectsAllPlans" : 4,
        "nscannedAllPlans" : 4,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 0,
        "indexBounds" : {
                "for" : [
                        [
                                "ac3",
                                "ac3"
                        ]
                ]
        },
        "server" : "mongodb.bezha.od.ua:27017"
}

Статистика

> db.products.stats()
{
        "ns" : "pcat.products",
        "count" : 12,
        "size" : 2356,
        "avgObjSize" : 196.33333333333334,
        "storageSize" : 8192,
        "numExtents" : 1,
        "nindexes" : 1,
        "lastExtentSize" : 8192,
        "paddingFactor" : 1.003000000000001,
        "systemFlags" : 0,
        "userFlags" : 0,
        "totalIndexSize" : 8176,
        "indexSizes" : {
                "_id_" : 8176
        },
        "ok" : 1
}
> use admin
switched to db admin
>
> db.runCommand( {listDatabases :1 } )
{
        "databases" : [
                {
                        "name" : "m101",
                        "sizeOnDisk" : 218103808,
                        "empty" : false
                },
                {
                        "name" : "pcat",
                        "sizeOnDisk" : 218103808,
                        "empty" : false
                },
                {
                        "name" : "admin",
                        "sizeOnDisk" : 1,
                        "empty" : true
                },
                {
                        "name" : "local",
                        "sizeOnDisk" : 1,
                        "empty" : true
                },
                {
                        "name" : "test",
                        "sizeOnDisk" : 1,
                        "empty" : true
                }
        ],
        "totalSize" : 436207616,
        "ok" : 1
}