How to get all the records in mongodb having object id

Multi tool use
How to get all the records in mongodb having object id
In the mongodb there is a user data has been stored in collection challange
the data seems like bellow:
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"
}
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.
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
@flimzy these answers are right if we run this in the mongodb shell but i need it to make as a query in golang
– ansh
Jul 3 at 7:05
3 Answers
3
'_id'
and '$type'
are invalid rune literals, you can't list multiple runes (characters) in a rune literal (only a single rune).
'_id'
'$type'
The bson.M
type is a map with string
key type, so you have to use string literals (or expressions), like this:
bson.M
string
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:
bson
conditions := bson.M{"_id": bson.M{"$type": bson.ElementObjectId}}
Thanks for this awesome answer
– ansh
Jul 3 at 8:20
You can use $type operator:
db.challenge.find({ _id: { $type: "objectId" } })
then how it will written in go can you tell me sir @mickl
– ansh
Jul 3 at 5:27
@ansh there was nothing about go in your question :) You need to read the docs, maybe here: godoc.org/labix.org/v2/mgo#Collection.Find
– mickl
Jul 3 at 5:46
see I'm using the Query like that ` conditions := bson.M{'_id': bson.M{'$type': "objectId" } } data, err := models.GetAllUsers(conditions)`
– ansh
Jul 3 at 5:58
You can try like below
//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
}
}
]
}
)
Refer $type , $or
can you please tell me how i will write the query in go lang
– ansh
Jul 3 at 6:43
refer this document goinbigdata.com/…
– Ratan Uday Kumar
Jul 3 at 6:46
see @ratanUdayKumar I'm also writing it like that but it gives me error
conditions := bson.M{'_id': bson.M{'$type': "objectId" } } data, err := models.GetAllUsers(conditions)
– ansh
Jul 3 at 6:51
conditions := bson.M{'_id': bson.M{'$type': "objectId" } } data, err := models.GetAllUsers(conditions)
update your code in question
– Ratan Uday Kumar
Jul 3 at 6:51
see now I update my code
– ansh
Jul 3 at 6:57
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
No. I've never used MongoDB. I don't know why you assume that someone who improves your formatting would be a subject matter expert on your specific question. But you already have multiple answers.
– Flimzy
Jul 3 at 7:02