【问题标题】:How to get all the records in mongodb having object id如何获取具有对象ID的mongodb中的所有记录
【发布时间】:2018-12-11 07:49:52
【问题描述】:

在 mongodb 中有一个用户数据已存储在集合 challange 中,数据如下所示:

{
"_id" : 1,
 "name" : "puneet",
 "last" : "jindal",
 "email" : "puneet@g.com"
}
{
 "_id" : ObjectId("5b3af82cdb3aaa47792b5fd3"),
 "name" : "hardeep",
 "last" : "singh",
 "email" : "hardeep@g.com"
}
{ 
 "_id" : 3,
 "name" : "gaurav",
 "last" : "bansal",
 "email" : "gaurav@g.com"
}
{
 "_id" : ObjectId("5b3af87ddb3aaa47792b5fd4"),
 "name" : "manish",
 "last" : "jindal",
 "email" : "manish@g.com"
}

在上面的数据中,有四个记录,其中两个具有整数形式的 id,另一个具有对象形式的 id。我只想检索在 id 字段中具有object id 的所有记录。谁能告诉我应该在代码中编写什么查询,只检索具有对象 id 的记录。

更新:

我正在使用的代码:

type User struct {
 Id              bson.ObjectId    `json:"_id" bson:"_id,omitempty"`
 Name            string    `json:"name,omitempty" bson:"name,omitempty"`
 Last            string `json:"last,omitempty" bson:"last,omitempty"`
 Email          string `json:"email,omitempty" bson:"email,omitempty"`
}
type Users []User

func GetAllUsers(listQuery interface{}) (result Users, err error) {
 mongoSession := config.ConnectDb()
 sessionCopy := mongoSession.Copy()
 defer sessionCopy.Close()
 getCollection := mongoSession.DB(config.Database).C(config.UsersCollection)
 err = getCollection.Find(listQuery).Select(bson.M{"password": 0}).All(&result)
 if err != nil {
    return result, err
 }
 return result, nil
}

conditions := bson.M{'_id': bson.M{'$type': "objectId" } }
data, err := models.GetAllUsers(conditions) 

我使用这个遇到的错误:-

controllers/UserController.go:18:23: 无效字符文字(超过一个字符) controllers/UserController.go:18:28: 不能使用 '\u0000' (type rune) 作为映射键中的类型字符串

【问题讨论】:

  • 没有。我从来没有使用过 MongoDB。我不知道您为什么认为改进格式的人会成为您特定问题的主题专家。但你已经有多个答案了。
  • @flimzy 如果我们在 mongodb shell 中运行它,这些答案是正确的,但我需要它作为 golang 中的查询

标签: mongodb go mongodb-query mgo


【解决方案1】:

您可以使用$type 运算符:

db.challenge.find({ _id: { $type: "objectId" } })

【讨论】:

  • 那么它将如何写在 go 你能告诉我先生@mickl
  • @ansh 你的问题没有任何关于 go 的内容 :) 你需要阅读文档,也许在这里:godoc.org/labix.org/v2/mgo#Collection.Find
  • 看到我正在使用这样的查询 ` conditions := bson.M{'_id': bson.M{'$type': "objectId" } } data, err := models。 GetAllUsers(条件)`
【解决方案2】:

你可以像下面这样尝试

//For Retrieving for ObjectID
db.challange.find(
    {
        "_id": {
            $type: 7  //ObjectID
        }
    }
)

//For Retrieving for Number
db.challange.find(
    {
        $or: [
            {
                "_id": {
                    $type: 1  //double
                }
            },
            {
                "_id": {
                    $type: 16  //32 bit integer
                }
            },
            {
                "_id": {
                    $type: 18  //64 bit integer
                }
            },
            {
                "_id": {
                    $type: 19  //decimal
                }
            }
        ]
    }
)

参考$type,$or

【讨论】:

  • 你能告诉我如何用 go lang 编写查询
  • 参考本文档goinbigdata.com/…
  • 见@ratanUdayKumar 我也是这样写的,但它给了我错误conditions := bson.M{'_id': bson.M{'$type': "objectId" } } data, err := models.GetAllUsers(conditions)
【解决方案3】:

'_id''$type' 无效rune literals,你不能在一个符文文字中列出多个符文(字符)(只有一个符文)。

bson.M 类型是一个带有string 键类型的映射,所以你必须使用string literals(或表达式),像这样:

conditions := bson.M{"_id": bson.M{"$type": "objectId"}}

另请注意,bson 包包含不同类型的 constants,因此使用这些常量更安全:

conditions := bson.M{"_id": bson.M{"$type": bson.ElementObjectId}}

【讨论】:

  • 感谢您的精彩回答
猜你喜欢
  • 2021-07-22
  • 1970-01-01
  • 2017-10-29
  • 2016-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多