gpt4 book ai didi

database - 如何使用 bool 属性检查运行查询

转载 作者:IT王子 更新时间:2023-10-29 02:18:47 24 4
gpt4 key购买 nike

文件夹文档:

{"_id":"5d1e2da512ad38225af60869","id":1,"name":"inbox","must":true}
{"_id":"5d1e2da512ad38225af6086b","id":2,"name":"outbox","must":true}
{"_id":"5d1e2da512ad38225af6086d","id":3,"name":"drafts","must":true}
{"_id":"5d1e2da512ad38225af6086f","id":4,"name":"trash","must":true}

邮件文件:

{"_id":"5d1e2da512ad38225af60871","id":1,"isdeleted":true,"foldersids":[1],"subject":"Hello1","body":"Hello this is mail 1"}
{"_id":"5d1e2da512ad38225af60873","id":2,"isdeleted":false,"foldersids":[1],"subject":"Hello2","body":"Hello this is mail 2"}

我试图获取所有未标记为已删除的邮件,并仅获取它们的 Id 值,这是一个单独的字段(不是 object_id 字段)

在我的数据库中,我有 2 个文档 - 1 个标记为已删除,1 个标记为未删除。这段代码总是返回这两个文件,尽管预期的结果是它只返回未删除文件的 I'd。什么会导致它失败?

编辑:完整示例

我的 golang 代码:

Folder.go

package main

type Folder struct {
Id int
Name string
Must bool
}

Mail.go

package main

type Mail struct{
Id int
IsDeleted bool
FoldersIds []int
Subject string
Body string
}

ma​​in.go

package main

import (
"fmt"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
)

func main(){
inbox := Folder{
Id:1,
Name:"inbox",
Must:true,
}
outbox := Folder{
Id:2,
Name:"outbox",
Must:true,
}
drafts := Folder{
Id:3,
Name:"drafts",
Must:true,
}
trash := Folder{
Id:4,
Name:"trash",
Must:true,
}

session, _ := mgo.Dial("localhost:27017")

session.DB("Outlook").C("Folders").Upsert(bson.M{"id": inbox.Id}, inbox)
session.DB("Outlook").C("Folders").Upsert(bson.M{"id": outbox.Id}, outbox)
session.DB("Outlook").C("Folders").Upsert(bson.M{"id": drafts.Id}, drafts)
session.DB("Outlook").C("Folders").Upsert(bson.M{"id": trash.Id}, trash)

mail1 := Mail{Id:1,Body:"Hello this is mail 1", FoldersIds:[]int{1}, IsDeleted:true, Subject:"Hello1"}
mail2 := Mail{Id:2,Body:"Hello this is mail 2", FoldersIds:[]int{1}, IsDeleted:false, Subject:"Hello2"}

session.DB("Outlook").C("Mails").Upsert(bson.M{"id": 1}, mail1)
session.DB("Outlook").C("Mails").Upsert(bson.M{"id": 2}, mail2)

var mails []Mail

session.DB("Outlook").C("Mails").Find(bson.M{"IsDeleted": bson.M{"$ne": true}}).Select(bson.M{"Id": 1}).All(&mails)

fmt.Println(len(mails)) // Why is this 2 and not 1?????
}

最佳答案

MongoDB 文档字段的名称区分大小写。您在数据库中的文档具有小写的字段名称,例如已删除id。所以你必须在构造你的过滤器时写同样的东西:

session.DB("Outlook").C("Mails").Find(bson.M{
"isdeleted": bson.M{"$ne": true},
}).Select(bson.M{"id": 1}).All(&mails)

如果你想改变名字在数据库中的保存方式,使用bson struct tag ,例如:

type Mail struct {
Id int `bson:"Id"`
IsDeleted bool `bson:"IsDeleted"`
FoldersIds []int `bson:"FolderIds"`
Subject string `bson:"Subject"`
Body string `bson:"Body"`
}

通常 Camel case用于由多个单词组成的名称:

type Mail struct {
Id int `bson:"id"`
IsDeleted bool `bson:"isDeleted"`
FoldersIds []int `bson:"folderIDs"`
Subject string `bson:"subject"`
Body string `bson:"body"`
}

但是您可以使用任何命名策略,都可以,但要保持一致。

关于database - 如何使用 bool 属性检查运行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56890608/

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