gpt4 book ai didi

mongodb - 使用 native 驱动程序附加到mongo查询

转载 作者:行者123 更新时间:2023-12-03 10:09:12 27 4
gpt4 key购买 nike

我有以下代码:

type SearchReq struct {
PageSize int32
Offset int32
Zipcode string
PracticeType []string
TreatmentType []string
HasPhotos bool
LatLong GeoLocationInput
}

func buildSearchQuery(searchOptions *database.SearchReq) bson.D {
var filter bson.D

if searchOptions.Zipcode != "" {
filter = append(filter, bson.E{"zipcode", searchOptions.Zipcode})
}

if len(searchOptions.PracticeType) != 0 {
filter = append(filter, bson.E{"practicetypes", bson.E{"$all", searchOptions.PracticeType}})
}

if len(searchOptions.TreatmentType) != 0 {
filter = append(filter, bson.E{"treatments", bson.E{"$all", searchOptions.TreatmentType}})
}

if searchOptions.HasPhotos {
filter = append(filter, bson.E{"avatarurl", bson.E{"$ne", nil}})
}

return filter
}
我尝试进行的查询之一如下所示:
db.providers.find({treatments: {$all: ["botox","not-botox"]}, zipcode: "90210"})
过滤器将输出以下内容:
1st run: [{zipcode 60010} {avatarurl {$ne <nil>}}]
2nd run: [{zipcode 90210} {treatments {$all [botox not-botox]}}]
该命令行查询会提取结果,而Go变体则不会。我尝试将其切换到 map ,但无济于事。我在这里做错了什么?
理想情况下,我正在尝试构建一种向一个查询添加一组条件的方法。
PS。命令行查询中的记录
{ "_id" : ObjectId("6001d1aab756f9540e8850ba"), "name" : "Mr. Brandy Emmerich", "accountnumber" : "b0c6ee03ab", "displayname" : "Mr. Brandy Emmerich", "streetaddress" : "92813 Funk Isle", "city" : "New Artville", "statename" : "Oklahoma", "zipcode" : "90210", "websiteurl" : "http://wildermannikolaus.org/robert.wolf", "emailaddress" : "hosea.runte@example.com", "telephonenumber" : "", "hours" : [ { "day" : 0, "open" : ISODate("2021-01-15T17:32:26.383Z"), "close" : ISODate("2021-01-15T17:32:26.383Z") }, { "day" : 1, "open" : ISODate("2021-01-15T17:32:26.383Z"), "close" : ISODate("2021-01-15T17:32:26.383Z") }, { "day" : 2, "open" : ISODate("2021-01-15T17:32:26.383Z"), "close" : ISODate("2021-01-15T17:32:26.383Z") }, { "day" : 3, "open" : ISODate("2021-01-15T17:32:26.383Z"), "close" : ISODate("2021-01-15T17:32:26.383Z") }, { "day" : 4, "open" : ISODate("2021-01-15T17:32:26.383Z"), "close" : ISODate("2021-01-15T17:32:26.383Z") }, { "day" : 5, "open" : ISODate("2021-01-15T17:32:26.383Z"), "close" : ISODate("2021-01-15T17:32:26.383Z") }, { "day" : 6, "open" : ISODate("2021-01-15T17:32:26.383Z"), "close" : ISODate("2021-01-15T17:32:26.383Z") } ], "instagramurl" : "https://instagram.com/ilene.kunde", "twitterurl" : "https://twitter.com/ilene.kunde", "facebookurl" : "https://facebook.com/ilene.kunde", "avatarurl" : "http://wehner.name/nick", "treatments" : [ "botox", "not-botox" ], "practicetypes" : null, "tags" : [ "", "", "", "id", "delectus", "pariatur" ], "metadata" : { "dolorem" : "excepturi", "numquam" : "at" }, "created_at" : ISODate("2021-01-15T17:32:26.383Z") }
{ "_id" : ObjectId("6001d1d0701489602c55f82a"), "name" : "Clara McLaughlin", "accountnumber" : "964a3de178", "displayname" : "Clara McLaughlin", "streetaddress" : "8708 Rodolfo Prairie", "city" : "Parisianburgh", "statename" : "West Virginia", "zipcode" : "90210", "websiteurl" : "http://pollich.org/kimberly_rau", "emailaddress" : "emiliano@example.org", "telephonenumber" : "", "hours" : [ { "day" : 0, "open" : ISODate("2021-01-15T17:33:04.526Z"), "close" : ISODate("2021-01-15T17:33:04.526Z") }, { "day" : 1, "open" : ISODate("2021-01-15T17:33:04.526Z"), "close" : ISODate("2021-01-15T17:33:04.526Z") }, { "day" : 2, "open" : ISODate("2021-01-15T17:33:04.526Z"), "close" : ISODate("2021-01-15T17:33:04.526Z") }, { "day" : 3, "open" : ISODate("2021-01-15T17:33:04.526Z"), "close" : ISODate("2021-01-15T17:33:04.526Z") }, { "day" : 4, "open" : ISODate("2021-01-15T17:33:04.526Z"), "close" : ISODate("2021-01-15T17:33:04.526Z") }, { "day" : 5, "open" : ISODate("2021-01-15T17:33:04.526Z"), "close" : ISODate("2021-01-15T17:33:04.526Z") }, { "day" : 6, "open" : ISODate("2021-01-15T17:33:04.526Z"), "close" : ISODate("2021-01-15T17:33:04.526Z") } ], "instagramurl" : "https://instagram.com/presley", "twitterurl" : "https://twitter.com/presley", "facebookurl" : "https://facebook.com/presley", "avatarurl" : "http://baumbach.com/minerva", "treatments" : [ "botox", "not-botox" ], "practicetypes" : null, "tags" : [ "", "", "", "corporis", "eveniet", "velit" ], "metadata" : { "enim" : "nisi" }, "created_at" : ISODate("2021-01-15T17:33:04.526Z") }

最佳答案

问题是如何添加复合过滤器。如果它们不是单个值,例如您正在使用$all$ne,则必须使用“完整”文档作为其值。 bson.E不是“完整”文档,它只是文档的元素。完整的文档为bson.Dbson.M
因此,将其用作过滤器生成器:

func buildSearchQuery(searchOptions *database.SearchReq) bson.D {
var filter bson.D

if searchOptions.Zipcode != "" {
filter = append(filter, bson.E{"zipcode", searchOptions.Zipcode})
}

if len(searchOptions.PracticeType) != 0 {
filter = append(filter, bson.E{"practicetypes", bson.D{{"$all", searchOptions.PracticeType}}})
}

if len(searchOptions.TreatmentType) != 0 {
filter = append(filter, bson.E{"treatments", bson.D{{"$all", searchOptions.TreatmentType}}})
}

if searchOptions.HasPhotos {
filter = append(filter, bson.E{"avatarurl", bson.D{{"$ne", nil}}})
}

return filter
}
另请注意,如果 searchOptions为空,则上面的代码将返回 nil,它作为过滤器无效。确保将其初始化为非 nil空片。所以代替
var filter bson.D
使用
filter := bson.D{}
还要注意,使用 bson.M而不是 bson.D更简单:
func buildSearchQueryMap(searchOptions *database.SearchReq) bson.M {
filter := bson.M{}

if searchOptions.Zipcode != "" {
filter["zipcode"] = searchOptions.Zipcode
}

if len(searchOptions.PracticeType) != 0 {
filter["practicetypes"] = bson.M{"$all": searchOptions.PracticeType}
}

if len(searchOptions.TreatmentType) != 0 {
filter["treatments"] = bson.M{"$all": searchOptions.TreatmentType}
}

if searchOptions.HasPhotos {
filter["avatarurl"] = bson.M{"$ne": nil}
}

return filter
}

关于mongodb - 使用 native 驱动程序附加到mongo查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65741101/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com