Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var client = new MongoClient();
- var database = client.GetDatabase("databasename");
- var collection = database.GetCollection<Customer>("Customer");
- var customerId = "..";
- var builder = Builders<Customer>.Filter;
- var filter = builder.Eq("Id", new ObjectId(customerId));
- var matchedRecord = collection.Find(filter).SingleOrDefault();
- // Insert new invoice into the customer record
- matchedRecord.Invoices.Add(new Invoice {
- InvoiceNumber = "123456"
- });
- collection.InsertOne(matchedRecord); // Error produced below
- E11000 duplicate key error collection: databasename.Customer index: _id_ dup key: { : ObjectId('..') }
- public class Customer
- {
- public ObjectId Id { get; set; }
- public string CustomerId { get; set; }
- public List<Invoice> Invoices { get; set; }
- }
- public class Invoice
- {
- public string InvoiceNumber { get; set; }
- }
- var builder = Builders<Customer>.Filter;
- var filter = builder.Eq("Id", new ObjectId(customerId));
- // Not sure how to use the FindOneAndUpdate method(?)
- var matchedRecord = collection.FindOneAndUpdate<Customer>(filter);
- var builder = Builders<Customer>.Filter;
- var filter = builder.Eq("Id", new ObjectId(customerId));
- var update = Builders<Customer>.Update.Set("Invoice.ClientName", "HELLO");
- var update = Builders<Customer>.Update
- .Set("Invoices", new Invoice
- {
- ClientName = "HELLO"
- });
- var result = collection.UpdateOneAsync(filter, update);
- var update = Builders<Customer>.Update.Push("Invoices", new Invoice
- {
- ClientName = "HELLO"
- });
- var result = collection.UpdateOneAsync(filter, update);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement