mongodb insert in array code example
Example 1: mongodb insert
db.products.insert( { item: "card", qty: 15 } )
Example 2: mongodb add to array if not exists
db.tags.update(
{name: 'sport'},
{$addToSet: { videoIDs: "34f54e34c" } }
);
Example 3: insert item into list mongodb
db.col.update(
{ name: 'doc', 'list.id': 2 },
{$push: {'list.$.items': {id: 5, name: 'item5'}}}
)
Example 4: F# mongodb insert to array
type FriendModel = {
Name : string
Phone : string
}
type PersonModel = {
Name : string
Friends: FriendModel list
}
let insertNewFriend (personName:string) (newFriend:FriendModel) =
let filter = Builders<PersonModel>.Filter.Eq((fun x -> x.Name), personName)
let fields = StringFieldDefinition<PersonModel>("Friends")
let updateDefinition = Builders<PersonModel>.Update.Push(fields,{Name="Mahdi";Phone="+1"})
collections.UpdateOne(filter,updateDefinition)