array içine insert

59 views
Skip to first unread message

Serdar B.

unread,
Dec 2, 2011, 5:33:58 AM12/2/11
to mong...@googlegroups.com
Merhaba,

MyCollection.Insert yada MyCollection.Save ile Array içine Item eklemeyi bir türlü başaramadım...

şöyle bir şema hayal ediyorum.

_id
MyItems
   MyItem
       MyItemId
       MyItemName
   MyItem
       MyItemId
       MyItemName


Bu şemaya yeni MyItem ekleme kodunu bir türlü yazamadım...
Yardımcı olabilecek var mı?

c# ile yazıyorum.

MyCollection.Save(new BsonDocument {
                       { "_id", MyObjetcs.Id },
                       { "MyItems", new BsonArray {
                           new BsonDocument {{ "MyItemId", x.Id }},
                           new BsonDocument {{ "MyItemName", x.Name }}
                       }}});

şu nu yazdığımda istediğim gibi bir insert olmuyor...,

Query oluşturup update denedim oda olmadı...

var query = Query.And(Query.EQ("_id", MyObjects.Id),
                                          Query.EQ("MyItems.MyItem.MyItemId", x.Id));


Nereleri yanlış düşünüyorum...


Serdar
--
serdarb.com

Veysel Taşçıoğlu

unread,
Dec 2, 2011, 6:41:23 AM12/2/11
to mong...@googlegroups.com, nosq...@googlegroups.com
Serdar merhaba;

_id
List<MyItem> myItems

İşini görecektir diye düşünüyorum. 

Kolay gelsin.



2011/12/2 Serdar B. <hser...@gmail.com>

Serdar B.

unread,
Dec 2, 2011, 7:08:29 AM12/2/11
to mong...@googlegroups.com
Merhaba,

Nesne zaten List içinde... ToBson() yapmadı o şekilde belki ben bişeyi gözden kaçırıyorum...

Stackoverflow'da şöyle bir yönlendirme geldi.
MyCollection.Update(Query.EQ("_id", MyObject.Id), 
                   
Update.PushWrapped("MyArray", myArrayField)

Oluyomuş gibi yapıyo ama _id'yi sona atıyo update sonrası... doğru yaptığıma emin olamadım o yüzden...


Serdar
--
serdarb.com


2011/12/2 Veysel Taşçıoğlu <veysel.t...@gmail.com>

Veysel Taşçıoğlu

unread,
Dec 2, 2011, 7:10:48 AM12/2/11
to mong...@googlegroups.com
"_id yi sona atıyor" derken?

Serdar B.

unread,
Dec 2, 2011, 7:15:15 AM12/2/11
to mong...@googlegroups.com
ilk insertteki find sorgusu 

{"_id" : "1" , "MyItems" ...... } iken

update sonrası

{ .... , "_id" :"1" } oluyor...

obsesif birine yapılcak şey değil yani :)

Veysel Taşçıoğlu

unread,
Dec 2, 2011, 7:17:29 AM12/2/11
to mong...@googlegroups.com, nosq...@googlegroups.com
_id nin yeri önemli değil, önemli olan insert edip, update edip vs.. sonra deserialize ederken veride bir sıkıntı olmaması.



2011/12/2 Serdar B. <hser...@gmail.com>

Hasan Tayyar BESIK

unread,
Dec 2, 2011, 7:22:42 AM12/2/11
to mong...@googlegroups.com
Serdar; evet konumu öndemli değil. Aslında field konumlarının önemli olduğu bir veritabanı var mı bimliyorum.
Ayrıca
http://www.mongodb.org/display/DOCS/Updating#Updating-Field%28re%29order  dan alıntı yaparak konuna bir açıklama getirebilirim : 

Field (re)order

During an update the field order may be changed. There is no guarantee that the field order will be consistent, or the same, after an update. At the moment, if the update can be applied in place then the order will be the same (with additions applied at the end), but if a move is required for the document (if the currently allocated space is not sufficient for the update) then the fields will be reordered (alphanumerically).





Hasan Tayyar BEŞİK
http://hasantayyar.com
http://twitter.com/htayyar

Please don't print this e-mail unless you really need to.

Serdar B.

unread,
Dec 2, 2011, 7:25:20 AM12/2/11
to mong...@googlegroups.com
Teşekkürler,

içime su serpildi :)

Serdar
--
serdarb.com

Veysel Taşçıoğlu

unread,
Dec 2, 2011, 7:27:09 AM12/2/11
to mong...@googlegroups.com

Yanlış hatırlamıyorsam (c# driverında) custom mapping yazılırsa sırası belirlenebiliyordu.
Ayrıca örneğin değişkenin ismi çok uzun ve "My...........List" gibi birşey diyelim. Bunu mapping ile store edilrken ismini örneğin "abc" diye değiştirebiliyoruz.



2011/12/2 Serdar B. <hser...@gmail.com>

Serdar B.

unread,
Dec 2, 2011, 7:31:54 AM12/2/11
to mong...@googlegroups.com
Evet varmış öyle bir attribute..
public class MyClass {
    [BsonElement("sp", Order = 1)]
    public string SomeProperty { get; set; }
}

Ben sürekli serialize/deseriliaze olmasın diye 
new BsonDocument deyip kendim map edeyim dedim...

ikisinin arasında bir fark yok diyebilir miyiz? orda da mı obsesif davranıyorum acaba...

Serdar
--
serdarb.com


2011/12/2 Veysel Taşçıoğlu <veysel.t...@gmail.com>

Veysel Taşçıoğlu

unread,
Dec 2, 2011, 7:34:31 AM12/2/11
to mong...@googlegroups.com
Attribute değil de, mapping kullanırsan daha iyi olur diye düşünüyorum.



2011/12/2 Serdar B. <hser...@gmail.com>
Reply all
Reply to author
Forward
0 new messages