dsfhe34889789708 2018-07-03 05:16
浏览 710
已采纳

如何获取mongodb中具有对象ID的所有记录

In the mongodb there is a user data has been stored in collection challange the data seems like bellow:

{
"_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"
}

In the above data there are four records and two of them having id in the integer form and other will having id in object form. I just want to retrieve the all the records which are having the object id in the id field. can anyone tell what query should I write in the code that will only retrieve that records which are having the object id.

Updated:

code I'm using :

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) 

The error I'm facing by using this :-

controllers/UserController.go:18:23: invalid character literal (more than one character) controllers/UserController.go:18:28: cannot use '\u0000' (type rune) as type string in map key

  • 写回答

3条回答 默认 最新

  • dtpngq3378499 2018-07-03 07:33
    关注

    '_id' and '$type' are invalid rune literals, you can't list multiple runes (characters) in a rune literal (only a single rune).

    The bson.M type is a map with string key type, so you have to use string literals (or expressions), like this:

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

    Also note that the bson package holds constants for the different types, so it's safer to use those constants:

    conditions := bson.M{"_id": bson.M{"$type": bson.ElementObjectId}}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败