Inserting Dates Into Mongo with Golang
I am trying to create a Golang MongoDB connector that takes in a request from a client and updates/inserts the request body into a database. An example of the request body is:
{
"_id": {
"$oid": <hexOID>
},
"DateCreated": {
"$date": 1460091636474
},
"DateModified": {
"$date": 1542241349721
}
}
The current Mongo driver and BSON library I am using is the one located at github.com/globalsign/mgo/ and github.com/globalsign/mgo/bson respectively.
Whenever I try to unmarshal the above response, I get an error:
cannot parse date: "{rn "$date": 1460091636474rn }"
I've read around and saw some answers about creating a custom marshaller/unmarshaller but how would I go about doing that if it does solve this problem?
A subset of my code is as follows:
var update interface{}
errUpdate := bson.UnmarshalJSON(body, &update)
if errUpdate != nil {
fmt.Println(errUpdate)
}
dbErr = collection.Update(query, update)
I keep update as an interface since the request body that is passed constantly changes and is not well-defined.
mongodb go bson
add a comment |
I am trying to create a Golang MongoDB connector that takes in a request from a client and updates/inserts the request body into a database. An example of the request body is:
{
"_id": {
"$oid": <hexOID>
},
"DateCreated": {
"$date": 1460091636474
},
"DateModified": {
"$date": 1542241349721
}
}
The current Mongo driver and BSON library I am using is the one located at github.com/globalsign/mgo/ and github.com/globalsign/mgo/bson respectively.
Whenever I try to unmarshal the above response, I get an error:
cannot parse date: "{rn "$date": 1460091636474rn }"
I've read around and saw some answers about creating a custom marshaller/unmarshaller but how would I go about doing that if it does solve this problem?
A subset of my code is as follows:
var update interface{}
errUpdate := bson.UnmarshalJSON(body, &update)
if errUpdate != nil {
fmt.Println(errUpdate)
}
dbErr = collection.Update(query, update)
I keep update as an interface since the request body that is passed constantly changes and is not well-defined.
mongodb go bson
add a comment |
I am trying to create a Golang MongoDB connector that takes in a request from a client and updates/inserts the request body into a database. An example of the request body is:
{
"_id": {
"$oid": <hexOID>
},
"DateCreated": {
"$date": 1460091636474
},
"DateModified": {
"$date": 1542241349721
}
}
The current Mongo driver and BSON library I am using is the one located at github.com/globalsign/mgo/ and github.com/globalsign/mgo/bson respectively.
Whenever I try to unmarshal the above response, I get an error:
cannot parse date: "{rn "$date": 1460091636474rn }"
I've read around and saw some answers about creating a custom marshaller/unmarshaller but how would I go about doing that if it does solve this problem?
A subset of my code is as follows:
var update interface{}
errUpdate := bson.UnmarshalJSON(body, &update)
if errUpdate != nil {
fmt.Println(errUpdate)
}
dbErr = collection.Update(query, update)
I keep update as an interface since the request body that is passed constantly changes and is not well-defined.
mongodb go bson
I am trying to create a Golang MongoDB connector that takes in a request from a client and updates/inserts the request body into a database. An example of the request body is:
{
"_id": {
"$oid": <hexOID>
},
"DateCreated": {
"$date": 1460091636474
},
"DateModified": {
"$date": 1542241349721
}
}
The current Mongo driver and BSON library I am using is the one located at github.com/globalsign/mgo/ and github.com/globalsign/mgo/bson respectively.
Whenever I try to unmarshal the above response, I get an error:
cannot parse date: "{rn "$date": 1460091636474rn }"
I've read around and saw some answers about creating a custom marshaller/unmarshaller but how would I go about doing that if it does solve this problem?
A subset of my code is as follows:
var update interface{}
errUpdate := bson.UnmarshalJSON(body, &update)
if errUpdate != nil {
fmt.Println(errUpdate)
}
dbErr = collection.Update(query, update)
I keep update as an interface since the request body that is passed constantly changes and is not well-defined.
mongodb go bson
mongodb go bson
edited Nov 16 '18 at 21:18
Riaru Neimu
asked Nov 16 '18 at 18:52
Riaru NeimuRiaru Neimu
153
153
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
First of all, the format of JSON in your example is called MongoDB Extended JSON. This is important, as you need to follow the format for extended JSON date. It's either $date with ISO-8601 string, or $date with $numberLong with Unix Epoch.
Another note is with Update you need an update operation i.e. $set. If you're intending to replace the document, use Replace. Although replacing _id doesn't quite make sense, the example below will use Replace as assumed from the example.
Given an example document in database as below:
db.collection.insert({
"_id": ObjectId("5bf36072a5820f6e28a4736c"),
"Foo":1
})
An alternative to using globalsign/mgo is to use mongo-go-driver/bson package, there's a method UnmarshalExtJSON() that you could easily utilise to parse extended JSON.
Using the current version (0.0.18), an example would be:
import (
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/mongo"
)
replacement := bson.D{}
// Example of response body
data := `{"_id":{"$oid":"5bf36072a5820f6e28a4736c"},"DateModified":{"$date":{"$numberLong":"1542241349721"}},"DateCreated":{"$date":{"$numberLong":"1460091636474"}}}`
err = bson.UnmarshalExtJSON(byte(data), true, &replacement)
if err != nil {
log.Fatal(err)
}
query := bson.D{{"Foo", 1}}
replaceResult, err := c.ReplaceOne(context.Background(), query, replacement)
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%2f53343782%2finserting-dates-into-mongo-with-golang%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
First of all, the format of JSON in your example is called MongoDB Extended JSON. This is important, as you need to follow the format for extended JSON date. It's either $date with ISO-8601 string, or $date with $numberLong with Unix Epoch.
Another note is with Update you need an update operation i.e. $set. If you're intending to replace the document, use Replace. Although replacing _id doesn't quite make sense, the example below will use Replace as assumed from the example.
Given an example document in database as below:
db.collection.insert({
"_id": ObjectId("5bf36072a5820f6e28a4736c"),
"Foo":1
})
An alternative to using globalsign/mgo is to use mongo-go-driver/bson package, there's a method UnmarshalExtJSON() that you could easily utilise to parse extended JSON.
Using the current version (0.0.18), an example would be:
import (
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/mongo"
)
replacement := bson.D{}
// Example of response body
data := `{"_id":{"$oid":"5bf36072a5820f6e28a4736c"},"DateModified":{"$date":{"$numberLong":"1542241349721"}},"DateCreated":{"$date":{"$numberLong":"1460091636474"}}}`
err = bson.UnmarshalExtJSON(byte(data), true, &replacement)
if err != nil {
log.Fatal(err)
}
query := bson.D{{"Foo", 1}}
replaceResult, err := c.ReplaceOne(context.Background(), query, replacement)
add a comment |
First of all, the format of JSON in your example is called MongoDB Extended JSON. This is important, as you need to follow the format for extended JSON date. It's either $date with ISO-8601 string, or $date with $numberLong with Unix Epoch.
Another note is with Update you need an update operation i.e. $set. If you're intending to replace the document, use Replace. Although replacing _id doesn't quite make sense, the example below will use Replace as assumed from the example.
Given an example document in database as below:
db.collection.insert({
"_id": ObjectId("5bf36072a5820f6e28a4736c"),
"Foo":1
})
An alternative to using globalsign/mgo is to use mongo-go-driver/bson package, there's a method UnmarshalExtJSON() that you could easily utilise to parse extended JSON.
Using the current version (0.0.18), an example would be:
import (
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/mongo"
)
replacement := bson.D{}
// Example of response body
data := `{"_id":{"$oid":"5bf36072a5820f6e28a4736c"},"DateModified":{"$date":{"$numberLong":"1542241349721"}},"DateCreated":{"$date":{"$numberLong":"1460091636474"}}}`
err = bson.UnmarshalExtJSON(byte(data), true, &replacement)
if err != nil {
log.Fatal(err)
}
query := bson.D{{"Foo", 1}}
replaceResult, err := c.ReplaceOne(context.Background(), query, replacement)
add a comment |
First of all, the format of JSON in your example is called MongoDB Extended JSON. This is important, as you need to follow the format for extended JSON date. It's either $date with ISO-8601 string, or $date with $numberLong with Unix Epoch.
Another note is with Update you need an update operation i.e. $set. If you're intending to replace the document, use Replace. Although replacing _id doesn't quite make sense, the example below will use Replace as assumed from the example.
Given an example document in database as below:
db.collection.insert({
"_id": ObjectId("5bf36072a5820f6e28a4736c"),
"Foo":1
})
An alternative to using globalsign/mgo is to use mongo-go-driver/bson package, there's a method UnmarshalExtJSON() that you could easily utilise to parse extended JSON.
Using the current version (0.0.18), an example would be:
import (
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/mongo"
)
replacement := bson.D{}
// Example of response body
data := `{"_id":{"$oid":"5bf36072a5820f6e28a4736c"},"DateModified":{"$date":{"$numberLong":"1542241349721"}},"DateCreated":{"$date":{"$numberLong":"1460091636474"}}}`
err = bson.UnmarshalExtJSON(byte(data), true, &replacement)
if err != nil {
log.Fatal(err)
}
query := bson.D{{"Foo", 1}}
replaceResult, err := c.ReplaceOne(context.Background(), query, replacement)
First of all, the format of JSON in your example is called MongoDB Extended JSON. This is important, as you need to follow the format for extended JSON date. It's either $date with ISO-8601 string, or $date with $numberLong with Unix Epoch.
Another note is with Update you need an update operation i.e. $set. If you're intending to replace the document, use Replace. Although replacing _id doesn't quite make sense, the example below will use Replace as assumed from the example.
Given an example document in database as below:
db.collection.insert({
"_id": ObjectId("5bf36072a5820f6e28a4736c"),
"Foo":1
})
An alternative to using globalsign/mgo is to use mongo-go-driver/bson package, there's a method UnmarshalExtJSON() that you could easily utilise to parse extended JSON.
Using the current version (0.0.18), an example would be:
import (
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/mongo"
)
replacement := bson.D{}
// Example of response body
data := `{"_id":{"$oid":"5bf36072a5820f6e28a4736c"},"DateModified":{"$date":{"$numberLong":"1542241349721"}},"DateCreated":{"$date":{"$numberLong":"1460091636474"}}}`
err = bson.UnmarshalExtJSON(byte(data), true, &replacement)
if err != nil {
log.Fatal(err)
}
query := bson.D{{"Foo", 1}}
replaceResult, err := c.ReplaceOne(context.Background(), query, replacement)
answered Nov 21 '18 at 6:32
Wan BachtiarWan Bachtiar
8,77231836
8,77231836
add a comment |
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%2f53343782%2finserting-dates-into-mongo-with-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