Пример #1
0
        public bool AddPicture(string owner, string name, string link)
        {
            Entities.Picture picture = new Entities.Picture {
                Link = link
            };
            pictureCollection.InsertOne(picture);
            var inserted = pictureCollection.Find(p => p.Link == link).First();

            var placeHolderID = new ObjectId("5eb01dda5cb63f469c3bc968");
            var album         = albumCollection.Find(a => a.AlbumName == name && a.Owner == owner).SingleOrDefault();

            if (album != null)
            {
                if (album.Pictures[0] == placeHolderID)
                {
                    var update = albumCollection.UpdateOne(
                        filter: p => p.AlbumName == name && p.Owner == owner,
                        update: Builders <Entities.Album> .Update.Pull(a => a.Pictures, placeHolderID),
                        options: new UpdateOptions {
                        IsUpsert = false
                    });
                }
            }


            var result = albumCollection.UpdateOne(
                filter: p => p.AlbumName == name && p.Owner == owner,
                update: Builders <Entities.Album> .Update.Push(a => a.Pictures, inserted.Id),
                options: new UpdateOptions {
                IsUpsert = false
            }
                );

            if (result.MatchedCount <= 0)
            {
                pictureCollection.DeleteOne(p => p.Id == inserted.Id);
                return(false);
            }
            return(true);
        }
Пример #2
0
        public bool AddPicture(string owner, string name, string link)
        {
            Entities.Picture picture = new Entities.Picture {
                Link = link
            };
            pictureCollection.InsertOne(picture);
            var inserted = pictureCollection.Find(p => p.Link == link).SingleOrDefault();

            var result = albumCollection.UpdateOne(
                filter: p => p.AlbumName == name && p.Owner == owner,
                update: Builders <Entities.Album> .Update.Push(a => a.Pictures, inserted.Id),
                options: new UpdateOptions {
                IsUpsert = false
            }
                );

            if (result.MatchedCount <= 0)
            {
                pictureCollection.DeleteOne(p => p.Id == inserted.Id);
                return(false);
            }
            return(true);
        }