SORU
31 Mayıs 2010, PAZARTESİ


İki tarih arasında MongoDB nesneleri bulmak

Mongodb içinde saklamak tweetler etrafında oynuyorum, her nesne bu gibi görünüyor:

{
"_id" : ObjectId("4c02c58de500fe1be1000005"),
"contributors" : null,
"text" : "Hello world",
"user" : {
    "following" : null,
    "followers_count" : 5,
    "utc_offset" : null,
    "location" : "",
    "profile_text_color" : "000000",
    "friends_count" : 11,
    "profile_link_color" : "0000ff",
    "verified" : false,
    "protected" : false,
    "url" : null,
    "contributors_enabled" : false,
    "created_at" : "Sun May 30 18:47:06  0000 2010",
    "geo_enabled" : false,
    "profile_sidebar_border_color" : "87bc44",
    "statuses_count" : 13,
    "favourites_count" : 0,
    "description" : "",
    "notifications" : null,
    "profile_background_tile" : false,
    "lang" : "en",
    "id" : 149978111,
    "time_zone" : null,
    "profile_sidebar_fill_color" : "e0ff92"
},
"geo" : null,
"coordinates" : null,
"in_reply_to_user_id" : 149183152,
"place" : null,
"created_at" : "Sun May 30 20:07:35  0000 2010",
"source" : "web",
"in_reply_to_status_id" : {
    "floatApprox" : 15061797850
},
"truncated" : false,
"favorited" : false,
"id" : {
    "floatApprox" : 15061838001
}

Nasıl kontrol eden bir sorgu yazıyorumcreated_atve tüm 18:47 ile 19:00 saatleri arasında nesneleri bulur? Tarihleri belirli bir formatta saklanır yani Belgelerim güncelleme gerek var mı?

CEVAP
31 Mayıs 2010, PAZARTESİ


Querying for a Date Range (Specific Month or Day)MongoDB Cookbookbu konuda çok iyi bir açıklama var, ama aşağıda kendimi denedim ve çalışmak gibi bir şey.

items.save({
    name: "example",
    created_at: ISODate("2010-04-30T00:00:00.000Z")
})
items.find({
    created_at: {
        $gte: ISODate("2010-04-29T00:00:00.000Z"),
        $lt: ISODate("2010-05-01T00:00:00.000Z")
    }
})
=> { "_id" : ObjectId("4c0791e2b9ec877893f3363b"), "name" : "example", "created_at" : "Sun May 30 2010 00:00:00 GMT 0300 (EEST)" }

Deneyimlerime dayanarak, aşağıdaki istenmeyen arama sonuçları verdiği için destekler MongoDB bir biçime tarihleri seri hale getirmek gerekir.

items.save({
    name: "example",
    created_at: "Sun May 30 18.49:00  0000 2010"
})
items.find({
    created_at: {
        $gte:"Mon May 30 18:47:00  0000 2015",
        $lt: "Sun May 30 20:40:36  0000 2010"
    }
})
=> { "_id" : ObjectId("4c079123b9ec877893f33638"), "name" : "example", "created_at" : "Sun May 30 18.49:00  0000 2010" }

İkinci örnekte sonuç bekleniyordu, ama yine de bir tane almış oldu. Bu temel bir dize karşılaştırma yapılır çünkü.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Ash100HD

    Ash100HD

    29 EKİM 2011
  • Awesome Toys TV

    Awesome Toys

    18 HAZİRAN 2014
  • Matt Stokes

    Matt Stokes

    22 Ocak 2008