Printing all records in a mongoDB Collection Golang
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I've got a MongoDB Collection collection := db.Collection("JobBacklog")
that I'm trying to print out into the console. The DB is in a Docker container and controlling it with a script written in Go.
From what I've been able to find from the mongo-go-driver https://godoc.org/github.com/mongodb/mongo-go-driver/mongo
there is a way to do this but my code keeps returning document is nil
when I know it isn't.
This is my code I'm using to try to iterate through a collection called JobBacklog
cur, err := collection.Find(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
defer cur.Close(context.Background())
for cur.Next(context.Background()) {
raw, err := cur.DecodeBytes()
if err != nil {
log.Fatal(err)
}
//print element data from collection
fmt.Println("Element", raw, x)
}
if err := cur.Err(); err != nil {
log.Fatal(err)
}
I expect it to print out the contents of the collection which are:
_id:5c2d34e36657ba3238374f9a
UID:"ALDK"
PROFILE:"B"
STATUS:"PENDING"
DEVICE:"2.2.2.2"
That is an example entry of the JobBacklog DB.
Full disclosure, the end goal for this is to find the last entry that was added to the collection, but I need to be able to read through the collection first.
I know I'm connected to the DB, I can add/find/delete entries, but the printing out of all in the collection is eluding me.
Any assistance is appreciated.
Thanks!
mongodb go
add a comment |
I've got a MongoDB Collection collection := db.Collection("JobBacklog")
that I'm trying to print out into the console. The DB is in a Docker container and controlling it with a script written in Go.
From what I've been able to find from the mongo-go-driver https://godoc.org/github.com/mongodb/mongo-go-driver/mongo
there is a way to do this but my code keeps returning document is nil
when I know it isn't.
This is my code I'm using to try to iterate through a collection called JobBacklog
cur, err := collection.Find(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
defer cur.Close(context.Background())
for cur.Next(context.Background()) {
raw, err := cur.DecodeBytes()
if err != nil {
log.Fatal(err)
}
//print element data from collection
fmt.Println("Element", raw, x)
}
if err := cur.Err(); err != nil {
log.Fatal(err)
}
I expect it to print out the contents of the collection which are:
_id:5c2d34e36657ba3238374f9a
UID:"ALDK"
PROFILE:"B"
STATUS:"PENDING"
DEVICE:"2.2.2.2"
That is an example entry of the JobBacklog DB.
Full disclosure, the end goal for this is to find the last entry that was added to the collection, but I need to be able to read through the collection first.
I know I'm connected to the DB, I can add/find/delete entries, but the printing out of all in the collection is eluding me.
Any assistance is appreciated.
Thanks!
mongodb go
add a comment |
I've got a MongoDB Collection collection := db.Collection("JobBacklog")
that I'm trying to print out into the console. The DB is in a Docker container and controlling it with a script written in Go.
From what I've been able to find from the mongo-go-driver https://godoc.org/github.com/mongodb/mongo-go-driver/mongo
there is a way to do this but my code keeps returning document is nil
when I know it isn't.
This is my code I'm using to try to iterate through a collection called JobBacklog
cur, err := collection.Find(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
defer cur.Close(context.Background())
for cur.Next(context.Background()) {
raw, err := cur.DecodeBytes()
if err != nil {
log.Fatal(err)
}
//print element data from collection
fmt.Println("Element", raw, x)
}
if err := cur.Err(); err != nil {
log.Fatal(err)
}
I expect it to print out the contents of the collection which are:
_id:5c2d34e36657ba3238374f9a
UID:"ALDK"
PROFILE:"B"
STATUS:"PENDING"
DEVICE:"2.2.2.2"
That is an example entry of the JobBacklog DB.
Full disclosure, the end goal for this is to find the last entry that was added to the collection, but I need to be able to read through the collection first.
I know I'm connected to the DB, I can add/find/delete entries, but the printing out of all in the collection is eluding me.
Any assistance is appreciated.
Thanks!
mongodb go
I've got a MongoDB Collection collection := db.Collection("JobBacklog")
that I'm trying to print out into the console. The DB is in a Docker container and controlling it with a script written in Go.
From what I've been able to find from the mongo-go-driver https://godoc.org/github.com/mongodb/mongo-go-driver/mongo
there is a way to do this but my code keeps returning document is nil
when I know it isn't.
This is my code I'm using to try to iterate through a collection called JobBacklog
cur, err := collection.Find(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
defer cur.Close(context.Background())
for cur.Next(context.Background()) {
raw, err := cur.DecodeBytes()
if err != nil {
log.Fatal(err)
}
//print element data from collection
fmt.Println("Element", raw, x)
}
if err := cur.Err(); err != nil {
log.Fatal(err)
}
I expect it to print out the contents of the collection which are:
_id:5c2d34e36657ba3238374f9a
UID:"ALDK"
PROFILE:"B"
STATUS:"PENDING"
DEVICE:"2.2.2.2"
That is an example entry of the JobBacklog DB.
Full disclosure, the end goal for this is to find the last entry that was added to the collection, but I need to be able to read through the collection first.
I know I'm connected to the DB, I can add/find/delete entries, but the printing out of all in the collection is eluding me.
Any assistance is appreciated.
Thanks!
mongodb go
mongodb go
edited Jan 3 at 20:16
henleyhoudini
asked Jan 3 at 20:11
henleyhoudinihenleyhoudini
277
277
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The error message "document is nil' is about the filter in the Find(). Change the line
cur, err := collection.Find(context.Background(), nil)
to
cur, err := collection.Find(context.Background(), bson.D{{}})
should work.
add a comment |
db.Collection("JobBacklog").find({},function(err,result){
console.log(result);
})
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Please take some time to read How to Answer. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion
– Simply Ged
Jan 4 at 5:54
The question is taggedGo
, the asker wants a Go solution. This is not valid Go code.
– icza
Jan 4 at 7:28
Thank you for your answer, but this isn't in Go and when I convert it to Go I get the same issue ofdocument is nil
– henleyhoudini
Jan 4 at 17:17
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029129%2fprinting-all-records-in-a-mongodb-collection-golang%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The error message "document is nil' is about the filter in the Find(). Change the line
cur, err := collection.Find(context.Background(), nil)
to
cur, err := collection.Find(context.Background(), bson.D{{}})
should work.
add a comment |
The error message "document is nil' is about the filter in the Find(). Change the line
cur, err := collection.Find(context.Background(), nil)
to
cur, err := collection.Find(context.Background(), bson.D{{}})
should work.
add a comment |
The error message "document is nil' is about the filter in the Find(). Change the line
cur, err := collection.Find(context.Background(), nil)
to
cur, err := collection.Find(context.Background(), bson.D{{}})
should work.
The error message "document is nil' is about the filter in the Find(). Change the line
cur, err := collection.Find(context.Background(), nil)
to
cur, err := collection.Find(context.Background(), bson.D{{}})
should work.
answered Jan 4 at 21:44
simagixsimagix
84026
84026
add a comment |
add a comment |
db.Collection("JobBacklog").find({},function(err,result){
console.log(result);
})
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Please take some time to read How to Answer. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion
– Simply Ged
Jan 4 at 5:54
The question is taggedGo
, the asker wants a Go solution. This is not valid Go code.
– icza
Jan 4 at 7:28
Thank you for your answer, but this isn't in Go and when I convert it to Go I get the same issue ofdocument is nil
– henleyhoudini
Jan 4 at 17:17
add a comment |
db.Collection("JobBacklog").find({},function(err,result){
console.log(result);
})
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Please take some time to read How to Answer. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion
– Simply Ged
Jan 4 at 5:54
The question is taggedGo
, the asker wants a Go solution. This is not valid Go code.
– icza
Jan 4 at 7:28
Thank you for your answer, but this isn't in Go and when I convert it to Go I get the same issue ofdocument is nil
– henleyhoudini
Jan 4 at 17:17
add a comment |
db.Collection("JobBacklog").find({},function(err,result){
console.log(result);
})
db.Collection("JobBacklog").find({},function(err,result){
console.log(result);
})
answered Jan 4 at 5:33
koteswararao pvkoteswararao pv
3119
3119
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Please take some time to read How to Answer. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion
– Simply Ged
Jan 4 at 5:54
The question is taggedGo
, the asker wants a Go solution. This is not valid Go code.
– icza
Jan 4 at 7:28
Thank you for your answer, but this isn't in Go and when I convert it to Go I get the same issue ofdocument is nil
– henleyhoudini
Jan 4 at 17:17
add a comment |
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Please take some time to read How to Answer. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion
– Simply Ged
Jan 4 at 5:54
The question is taggedGo
, the asker wants a Go solution. This is not valid Go code.
– icza
Jan 4 at 7:28
Thank you for your answer, but this isn't in Go and when I convert it to Go I get the same issue ofdocument is nil
– henleyhoudini
Jan 4 at 17:17
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Please take some time to read How to Answer. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion
– Simply Ged
Jan 4 at 5:54
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Please take some time to read How to Answer. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion
– Simply Ged
Jan 4 at 5:54
The question is tagged
Go
, the asker wants a Go solution. This is not valid Go code.– icza
Jan 4 at 7:28
The question is tagged
Go
, the asker wants a Go solution. This is not valid Go code.– icza
Jan 4 at 7:28
Thank you for your answer, but this isn't in Go and when I convert it to Go I get the same issue of
document is nil
– henleyhoudini
Jan 4 at 17:17
Thank you for your answer, but this isn't in Go and when I convert it to Go I get the same issue of
document is nil
– henleyhoudini
Jan 4 at 17:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029129%2fprinting-all-records-in-a-mongodb-collection-golang%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown