Giriş
mondod komutu
2. Collection İşlemleri
Collection ilişkisel veri tabanındaki table gibi düşünülebilir.
aggregate metodu
bulkWrite metodu
createCollection
db.createCollection(“myCollection”, { capped : true, autoIndexID : true, size : 6142800,
max : 10000 } )
deleteMany metodu
Birden fazla satırı siler
Örnek
db.student.deleteOne({_id:3}
//delete the document which contains id=3.
db.student.deleteMany({_id:{$lt:3}}
//delete the documents which contains id is less than 3.
deleteOne metodu
Bir satırı siler
find metodu
MongoDB read and write operations aren’t performed as part of an atomic transaction, solving this with the database’s atomic findAndModify operation becomes trivial,
findOne metoduAçıklaması
şöyleIf multiple documents satisfy the query, this method returns the first document according to the natural order which reflects the order of documents on the disk.
Örnek
db.collection.findOne({
$or: [
{"apple": "blah"},
{"orange": "blah"},
{"grape": "blah"}
]
})
getcollectionnames metoduVeri tabanında bir sürü collection olsun. Bu collection'lardan "account" isimli bir alan sahip olanları şöyle
buluruz.
db.getCollectionNames().forEach(function(collname) {
var count = db[collname].find({"account": {$exists: true}}).count();
if (count > 0) {
print(collname);
}
})
Bu Oracle veri tabanındaki şu sorguya denk
gelir.
SELECT * FROM ALL_TAB_COLUMNS WHERE COLUMN_NAME LIKE 'account';
insertOne
Örnek ver
insertMany
Örnek ver
insert metodu
Veriyi yaratmaya
çalışır. Veri mevcutsa hata verir. Şöyle bir verimiz
olsuncodes = [
{code:'abc'},
{code:'123'}
]
Bu veri şöyle eklenir.
db.collection.insert(codes)
Veri struct + array of struct olabilir. Şöyle
yaparız.
> db.coll.insert({
"settings": {
"system": "windows",
"date": "mm-dd-yyyy",
"time": "HH-MM-SS"
},
"info": [
{ "_id" : "99921", "city" : "CRAIG", "loc" : [ -133.117081, 55.47317 ]},
...
],
"Weather": [
{"_id":"99921", "weather": "cloudy"},
...
]
})
limit metoduDöndürülen kayıt sayısını
sınırlar. Genelde sort ile beraber kullanılması mantıklı
lowest = db.coll.find().sort({_id:1}).limit(1).next()._id;
min metoduElimizde şöyle kayıtlar
olsun.
[
{ product_name: "O",vendor_name: "test1", category: "F", date: "2015-06-12"},
{ product_name: "O",vendor_name: "test1", category: "F", date: "2015-02-24"},
{ product_name: "A",vendor_name: "test2", category: "F", date: "2015-07-11"},
{ product_name: "A",vendor_name: "test2", category: "F", date: "2015-06-19"}
]
En eski tarihli kayıtları şöyle
buluruz. Çıktı olarak 2 ve 4. kayıtı alırız.
db.wab.aggregate([{
$match : {
vendor_name : {
$in : ["test1", "test2"]
},
category : 'F'
}
}, {
$group : {
_id : {
vendor_name : "$vendor_name",
product_name : "$product_name"
},
business_date : {
$min : "$business_date"
}
}
}
])
updateOne
$unset ile kullanılırsa, dokümandan bir alan
siler
save metodudb.collection.save veriyi ya günceller ya da
yaratır.