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 1063493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:47:28+00:00 2026-05-16T18:47:28+00:00

I am using the MongoDB-Csharp driver and I was wondering what the proper way

  • 0

I am using the MongoDB-Csharp driver and I was wondering what the proper way to insert and query date field?

I tried using storing the dates using System.DateTime, but I am having issues when I try to query by date.

example:

Inserting Data

var mongo = new Mongo();
var db = mongo.GetDatabase(dbName);
var collection = db.GetCollection(collectionName);

var document = new Document();
document["date"] = DateTime.Now.ToUniversalTime();
collection.Save(document);

Querying Data

var mongo = new Mongo();
var db = mongo.GetDatabase(dbName);
var collection = db.GetCollection(collectionName);
var results = collection.Find(
new Document()
{
    {
        "date",
        new Document()
        {
            {
                "$lte", DateTime.Now.ToUniversalTime()
            }
        }
    }
}
);
  • 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-05-16T18:47:29+00:00Added an answer on May 16, 2026 at 6:47 pm

    As the MongoDB shell is a JavaScript shell, you need to use the JavaScript Date object:

    db.datetest.insert({"event": "New Year's Day 2011", "date": new Date(2011, 0, 1)});
    db.datetest.insert({"event": "Now", "date": new Date()});
    

    Note that if you pass year, month, date to the constructor the months start at 0.

    You can also pass a string to its constructor but it seems to ignore the locale so your date needs to be formatted US-style:

    db.datetest.insert({"event": "Christmas Day 2010", "date": new Date('12/25/2010')});
    

    Be sure to use new Date() rather than just Date(), because Date() just returns a string and you won’t be able to query it as a Date.

    The MongoDB-CSharp driver will convert the .NET DateTime object into a MongoDB Date object when it serializes it to BSON.

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

Sidebar

Related Questions

I am using MongoDB v1.4 and the mongodb-csharp driver and I try to group
Using MongoDB C# driver ( http://github.com/samus/mongodb-csharp ), seems that I'm unable to get the
Im using mongodb + java. I tried to insert a new document into an
I'm using mongodb 2.1 and how to translate this query into php db.counter.aggregate([ {
Using MongoDB and the latest 10gen C# driver (CSharpDriver-1.3.1.4349), I am trying to do
I'm using MongoDB but I'm not really using it's dynamic fields capabilities field :fb_followers
I'm using PHP and mongodb. I've turned on system profiling on everything above 20ms,
I am using Mongodb Java driver for my Java client application. it needs to
I'm using MongoDB in a reporting system and have to delete a whole bunch
Using mongodb with the NoRM driver I have this document: { _id : ObjectId(0758030341b870c019591900),

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.