Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8197685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:47:32+00:00 2026-06-07T05:47:32+00:00

This seems to be like a common use case… but somehow I cannot get

  • 0

This seems to be like a common use case… but somehow I cannot get it working.

I’m attempting to use MongoDB as an enumeration store with unique items. I’ve created a collection with a byte[] Id (the unique ID) and a timestamp (a long, used for enumeration). The store is quite big (terabytes) and distributed among different servers. I am able to re-build the store from scratch currently, since I’m still in the testing phase.

What I want to do is two things:

  1. Create a unique id for each item that I insert. This basically means that if I insert the same ID twice, MongoDB will detect this and give an error. This approach seems to work fine.
  2. Continuously enumerate the store for new items by other processes. The approach I took was to add a second index to InsertID and used a high precision timestamp on this along with the server id and a counter (just to make it unique and ascending).

In the best scenario this would mean that the enumerator would keep track of an index cursor for every server. From what I’ve learned from mongodb query processing I expected this behavior. However, when I try to execute the code (below) it seems to take forever to get anything.

        long lastid = 0;
        while (true)
        {
            DateTime first = DateTime.UtcNow;
            foreach (var item in collection.FindAllAs<ContentItem>().OrderBy((a)=>(a.InsertId)).Take(100))
            {
                lastid = item.InsertId;
            }
            Console.WriteLine("Took {0:0.00} for 100", (DateTime.UtcNow - first).TotalSeconds);
        }

I’ve read about cursors, but am unsure if they fulfill the requirements when new items are inserted into the store.

As I said, I’m not bound to any table structure or something like that… the only things that are important is that I can get new items over time and without getting duplicate items.

-Stefan.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:47:33+00:00Added an answer on June 7, 2026 at 5:47 am

    Somehow I figured it out… more or less…

    I created the query manually and ended up with something like this:

    db.documents.find({ “InsertId” : { “$gt” : NumberLong(“2020374866209304106”) } }).limit(10).sort({ “InsertId” : 1 });

    The LINQ query I put in the question doesn’t generate this query. After some digging in the code I found that it should be this LINQ query:

    foreach (var item in collection.AsQueryable().Where((a)=>(a.InsertId > lastid)).OrderBy((a) => (a.InsertId)).Take(100))

    The AsQueryable() seems to be the key to execute the rewriting of LINQ to MongoDB queries.

    This gives results, but still they appeared to be slow (4 secs for 10 results, 30 for 100). However, when I added ‘explain()’ I noticed ‘0 millis’ in the query execution.

    I stopped the process doing bulk inserts and tada, it works, and fast. In other words: the issues I was having were due to the locking behavior of MongoDB, and due to the way I interpreted the linq implementation. Since the former is the result of initial bulk-filling the data store, this means that the problem is solved.

    On the ‘negative’ part of the solution: I would have preferred a solution that involved serializable cursors or something like that… this ‘take’ solution has to iterate the b-tree over and over again. If someone has an answer for this, please let me know.

    -Stefan.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like it would be a common issue to be but I don't
This seems like a repeated question but i'm not able to get my answer.
This seems like such a simple issue but I cannot find an elegant solution.
This seems like I'm missing something obvious but I can't get redirects (>) to
This seems like the simplest Git question, but I can't find ANYTHING on it.
This seems like it should be relatively straightforward, I cannot figure out what I'm
This seems like a must have form input for a mobile ui framework, but
This seems like another fairly simple thing to do, but I'm again struggling on
This seems like it should be easy, but I can't quite find an explanation
It seems like this should be fairly obvious, but I can't find anything on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.