Created
September 16, 2012 14:56
-
-
Save fabriciocolombo/3732735 to your computer and use it in GitHub Desktop.
BSONArray modify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
procedure ReplaceArray; | |
var | |
vPerson: IBSONObject; | |
vAddresses, | |
vNewAddresses: IBSONArray; | |
begin | |
//Insert person | |
vAddresses := TBSONArray.NewFromValues(['one address', 'two address']); | |
vPerson := TBSONObject.NewFrom('id', 1).Put('name', 'fabricio').Put('addresses', vAddresses); | |
FCollection.Save(vPerson); | |
//Read from DB | |
vPerson := FCollection.FindOne(TBSONObject.NewFrom('id', 1)); | |
//replace entire old addresses array | |
vNewAddresses := TBSONArray.NewFromValues(['one', 'two']); | |
vPerson.Put('addresses', vNewAddresses); | |
//Update into DB | |
FCollection.Save(vPerson); | |
end; | |
procedure AdvancedArrayModify; | |
var | |
vPerson: IBSONObject; | |
vAddresses: IBSONArray; | |
begin | |
//Insert person | |
vAddresses := TBSONArray.NewFromValues(['one address', 'two address', 'three address']); | |
vPerson := TBSONObject.NewFrom('id', 1).Put('name', 'fabricio').Put('addresses', vAddresses); | |
FCollection.Save(vPerson); | |
//Read from DB | |
vPerson := FCollection.FindOne(TBSONObject.NewFrom('id', 1)); | |
//Replace second element | |
vAddresses.Put(1, 'one'); | |
//Insert new at first position | |
vAddresses.Insert(0, 'new'); | |
//Remove array element at index two | |
vAddresses.Remove(2); | |
//Update into DB | |
FCollection.Save(vPerson); | |
end; | |
procedure RemoveField; | |
var | |
vPerson: IBSONObject; | |
vAddresses: IBSONArray; | |
begin | |
//Insert person | |
vAddresses := TBSONArray.NewFromValues(['one address', 'two address']); | |
vPerson := TBSONObject.NewFrom('id', 1).Put('name', 'fabricio').Put('addresses', vAddresses); | |
FCollection.Save(vPerson); | |
//Read from DB | |
vPerson := FCollection.FindOne(TBSONObject.NewFrom('id', 1)); | |
//Remove field | |
vPerson.Remove('name'); | |
//Update into DB | |
FCollection.Save(vPerson); | |
end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment