0

I'm a newbie in a document-oriented database in general and in MongoDB in particular.

This database was created by me: a collection of several disjoint segments containing integers.

I'd like to take one item in accordance with some conditions and remove it from the document.

For example, I tried to take the item with conditions:

  1. from [-105; 17]
  2. not zero
  3. contains in {-104, -97, -5, 0, 5}

like this

var db          = client.GetDatabase("mongodbPOC");
var collection  = db.GetCollection<Document>("Int");
var contains    = new List<int> { -104, -97, -5, 0, 5 };
var result      = collection.AsQueryable()
                            .Where(document => 17 >= document.Min && -105 <= document.Max)
                            .SelectMany(document => document.Values)
                            .First(val => val != 0 && contains.Contains(val));

and find it again for remove, but I sure that exists a more profitable way to do that.

0

2 Answers 2

2

To remove items from array in MongoDb, you need to use Pull or PullFilter, in your case, you need to use PullFilter, like this:

var filterPull = Builders<int>.Filter
                              .Where(x => x != 0 && contains.Contains(x));
var update = Builders<YourModel>.Update
                                .PullFilter(c => c.Values, filterPull);

Then create another filter for Min, Max condition, this filter is for your document and use Update Collection:

var filter = Builders<YourModel>.Filter
                                .Where(document => 17 >= document.Min && -105 <= document.Max);
Collection.UpdateManyAsync(filter, update);
Sign up to request clarification or add additional context in comments.

5 Comments

How to get the taken and deleted value(value from result in my attempt)? And why you suggest UpdateManyAsync if the one item in one array is needed to delete?
And it doesn't work, it returns me System.AggregateException: "WriteEncodin", inner exception: InvalidOperationException: {document} is not supported.
UpdateManyAsync mean you can update multiple documents, it has nothing todo with your array, if you only need one item in one array, you need to change the filterPull
With this way, the only thing you can get is the number of affected documents by your query, if you want to get deleted values, your way is correct
when I replaced PullFilter to Pull it stars working, but it removes all elements from the array which fit the condition, but I want to remove only one element.
0

For remove finding a solution was not easy, but they helped me on the MongoDB forum in slack. To solve this problem there are two ways:

  1. using agg expressions in 4.2 for values where the position is known or unknown(Asya's answer): https://jira.mongodb.org/browse/SERVER-1014?focusedCommentId=2305681&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-2305681
  2. using $unset followed by $pullAll to remove all null's

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.