如何使用布尔属性检查运行查询

时间:2019-07-04 15:04:49

标签: database mongodb go mgo

文件夹文档:

{"_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。是什么导致它失败?

编辑:完整示例

我的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
}

main.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?????
}

1 个答案:

答案 0 :(得分:0)

MongoDB文档字段的名称区分大小写。您数据库中的文档具有小写的字段名称,例如isdeletedid。因此,构造过滤器时必须写相同的文字:

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"`
}

但是您可以使用任何命名策略,所有方法都可以工作,但是要保持一致。