I am having trouble finding information online as how to use Mongo ObjectID instances on the front-end.
I can't answer these questions:
(1) is it safe to serialize/deserialize ObjectID objects to/from JSON?
(2) How can I require the ObjectID module with AMD/RequireJS on the front-end?
(3) Is it better to just use strings on the front-end and just convert strings to ObjectIDs on the backend?
So yes I am having trouble working with and manipulating ObjectID objects on the frontend because I don't have the ObjectID module on the frontend, or at least this is a perceived problem. I haven't seen any examples of how to do this nor have I seen much talk of it online at all. Perhaps I am not approaching the problem correctly.
No. Your JSON parser will likely fail, as JSON only stores certain datatypes, and ObjectID isn't one of them...
Although, note that if you're stringifying your data it is possible that your MongoDB driver would in fact back this by returning a string from the ObjectID... Here is an example in NodeJS:
var ObjectID = require("mongodb").ObjectID,
myObject = {test:ObjectID("55153a8014829a865bbf700d")};
console.log(JSON.stringify(myObject));
// {"test":"55153a8014829a865bbf700d"}
No. I'm not sure there are any modules out there yet that give the ability to use ObjectID's in browser JS. Although perhaps you can port this NodeJS to browser JS compatibility?
Yes. For now, I would say yes. You can just use the string on the front-end; although, like I said ealier, if you can port the ObjectID peice to be browser compliant (which shouldn't be too hard), I don't think there would be any issues there.
Related
firstly, let's take this simple query as example:
ObjectId('62663def4e578b0a1cb482c5').valueOf();
output in DataGrip v2021.3.1:
{"$oid": "62663def4e578b0a1cb482c5"}
output in Navicat for MongoDB v16.0.14:
62663def4e578b0a1cb482c5
output in mongosh 1.5.0:
ObjectId("62663def4e578b0a1cb482c5")
secondly, with typeof operator, i.e.:
typeof ObjectId('62663def4e578b0a1cb482c5').valueOf();, we can see the data type of the output. they are object in both DataGrip and Mongosh but string in Navicat for MongoDb.
This is an obvious issue and problematic when developers write complex queries, I believe there must be tickets raised for similar issue(but i haven't found one).
Why it it like this? It could be due to different mongoDB driver version integrated in each client but I searched around and haven't found any solid official documents. Could anyone help on this?
mongosh and MongoDB JDBC driver that is used in DataGrip have the same core so they should be very similar (and your examples show that. {"$oid": "62663def4e578b0a1cb482c5"} is just json representation of ObjectId type).
I don't remember what Navicat uses. I might guess that they have a wrapper for MongoDB NodeJS driver or they directly use old shell called mongo.
I would say that mongosh should have the most correct behaviour since it's a new shell that was developed very recently. If you think that this behaviour is incorrect try to create an issue in mongosh issue tracker https://jira.mongodb.org/projects/MONGOSH/issues/
If you think that behaviour of Navicat is incorrect, try to create an issue in their bug tracker
I am currently working on a kotlin multi project solution.
I have one project defining some data classes and defining an api to access a mongodb. The objectId is created automatically. This project is using morphia:1.3.2.
Entries are stored using this function:
fun store(myClass: MyClass) = db.save(myClass).let { myClass.id?.toHexString() ?: "0" }
Now I'm using this project in a spring-boot kotlin project.
I created a small web page with some filters. These filters should be applied on my query. So far so good, everything is working.
The results of my query are returned via my Rest-controller without any conversions. In my web page I want to print the ObjectId foreach result.
But the ObjectId is not a String as it used to be, it is an object.
id:
counter:15304909
date:"2018-08-27T23:45:35.000+0000"
machineIdentifier:123456
processIdentifier:1234
time:1535413535000
timeSecond:1535413535
timestamp:1535413535
Is it possible to force morphia to return the objectId in the String representation? Or is there a on Option to activate the correct mapping? Or do I have to touch each result one by one and convert the object id to the hexadecimal string representation? I hope that there is a better, and quicker solution then this.
I am also not able to remap the object to a valid id, due to an java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986 exception. The request looks like this:
myClass?id={"timestamp":1535413631,"machineIdentifier":123456,"processIdentifier":1234,"counter":16576969,"time":1535413631000,"date":"2018-08-27T23:47:11.000+0000","timeSecond":1535413631}
I'm a little bit out of ideas, how to fix this issue.
Depending on your REST framework, you would need to provided a serializer for writing out that ObjectId as its String version, say. Most such frameworks make that transparent once it's configured so you need only worry about returning your objects out of your REST service and the framework will serialize properly.
I, personally, wouldn't muck about by trying to change how it's serialized in the database. ObjectId is a pretty good _id type and I wouldn't change it.
I am new to Jquery JSON. I wanted to know is it possible to use JSON as a database to store data and retrieve data whenever it is needed. Like instead of using mysql,or mssql or anything else is there any way to use only JSON(i.e. .json file)??
If yes can you please guide me? if no can anybody suggest a better way to store data?
And i need to know this for asp.net webforms.
You could use MongoDB instead of MySQL. MongoDB is based on "JSON" document storage. Example of CRUD with Mongo: insert and find data.
db.inventory.insert({
item: "ABC1",
details: {
model: "14Q3",
manufacturer: "XYZ Company"
}
})
db.inventory.find( { item: "ABC1" } )
From mongodb.com
The MongoDB BSON implementation is lightweight, fast and highly
traversable. Like JSON, MongoDB's BSON implementation supports
embedding objects and arrays within other objects and arrays – MongoDB
can even "reach inside" BSON objects to build indexes and match
objects against query expressions on both top-level and nested BSON
keys. This means that MongoDB gives users the ease of use and
flexibility of JSON documents together with the speed and richness of
a lightweight binary format. Read our MongoDB overview to learn more
about these features.
There is also a similar question on SO here
It is definitely possible and it depends, if it suits your requirement then go ahead and use mongodb. To have json as your datastore I would recommend to use MongoDB one of the popular NOSQL(Not only SQL) DB.
But if your data has lots of relation between different entities then it's not recommended to use MongoDB.
Go through this article, it gives you detailed explanation when to and when not to use NoSQL DB
http://www.sarahmei.com/blog/2013/11/11/why-you-should-never-use-mongodb/
How do I prevent an ObjectID form becoming primitive when passing in and out of Redis?
Parsing to JSON string? Thanks!
You can either store your object as JSON string representation in simple string or use hash data structure. Node.js node_redis module offers friendlier hash commands which may be useful. There is also a library for storing objects in redis called nohm which is based on object-hash mapping library ohm.
I am working on a web application that gets JSON data from the server (Ruby/Rails) into the client using jQuery/ajax and then renders it to the browser using jQuery to populate the DOM. To simplify access to my data on the client side, I would like to use an object-relational mapper similar to ActiveRecord, but which starts with JSON data instead of data directly from a SQL data source.
Are there any such ORMs in Javascript that convert a JSON data set (itself derived from a set of SQL queries on the server side) to a set of ActiveRecord-like objects?
I may be missing something here, but JSON (JavaScript Object Notation) is already a Javascript object in and of itself.
If the data you are getting from the server doesn't map well to a usable Javascript object, I would argue that it's the server side that needs to change to return a more useful serialized object rather than a simple recordset.
ExtJS has a very nice JsonStore class
There is CouchDB which is a DB written in Erlang that uses HTTP as the transport. This eliminates the need for middle-ware and permits you to navigate the DB directly with AJAX calls. I can't speak good or bad about it. I haven't heard much about it in months and it seems as if the hype train departed a few years ago.
You can't have an ORM to a remote DB in Javascript.. An, ORM requires transcendental knowledge of the DB schema, and sending that out with an API just isn't that pragmatic as of yet.
For persistent local storage, there is the now deprecated Google Gears and the HTML5 Clientside DB.
Yes there is JSON ODM. Exactly what you are looking for. If you need a method that is not supported yet post an issue and I'll try my best to support it as soon as possible.
If you like it please give it a star!