Comments (8)
You can run snippet install analyze-schema and then call schema() on collections and cursors: https://github.com/mongodb-labs/mongosh-snippets/tree/main/snippets/analyze-schema
So
snippet install
installs the module client side, right?
Yes, exactly 👍
Yes but let's say we provide
mongodb-schema
as a single file (for instance using https://github.com/vercel/ncc), then it would technically be possible to usemongodb-schema
withdb.eval()
?
Sure – I don’t think ncc is what you want, but bundlers in general could probably give you this functionality.
Out of curiosity, is there a better way than
db.eval()
? It seems that this (quite useful) function is deprecated:
I’m not sure, but generally, most server-side JS execution is at least discouraged. Maybe ask yourself, why do you need this to be server-side and not client-side?
from mongodb-schema.
Made https://jira.mongodb.org/browse/INT-317 to track background work to make this happen
from mongodb-schema.
@imlucas it seems that the issue no longer exists in JIRA:
@rueckstiess is there any news regarding this issue? I would be interested in using this library with PyMongo and MongoDB 5. Thanks!
from mongodb-schema.
Hi @Mogztter I don't think we need a mongo shell plugin for this library anymore now that the new mongo shell (mongosh) can import npm modules natively. I just tested the following to confirm:
- Install this module into your local working directory with
npm install mongodb-schema
- Open the (new) mongo shell with
mongosh
- Require the module with
schema = require('mongodb-schema')
- Pass in a list of documents, e.g.
test> schema([{a:1, b:1}, {a: 2}, {a:3, b: 3}])
{
fields: [
{
name: 'a',
path: 'a',
count: 3,
types: [
{
name: 'Number',
bsonType: 'Number',
path: 'a',
count: 3,
values: [ 1, 2, 3, pushSome: [Function (anonymous)] ],
total_count: 0,
probability: 1,
unique: 3,
has_duplicates: false
}
],
total_count: 3,
type: 'Number',
has_duplicates: false,
probability: 1
},
{
name: 'b',
path: 'b',
count: 2,
types: [
{
name: 'Number',
bsonType: 'Number',
path: 'b',
count: 2,
values: [ 1, 3, pushSome: [Function (anonymous)] ],
total_count: 0,
probability: 0.6666666666666666,
unique: 2,
has_duplicates: false
},
{
name: 'Undefined',
type: 'Undefined',
path: 'b',
count: 1,
total_count: 0,
probability: 0.3333333333333333,
unique: 1,
has_duplicates: false
}
],
total_count: 3,
type: [ 'Number', 'Undefined' ],
has_duplicates: false,
probability: 0.6666666666666666
}
],
count: 3
}
The integration could probably be improved. I wasn't able to pass in the cursor of a find() directly into the function, instead you'll have to convert it to an array first.
But I hope this will get you going in the mean time.
I'm unsure about your comment regarding PyMongo. This is a Javascript library and you won't be able to use it directly from Python though.
from mongodb-schema.
I don't think we need a mongo shell plugin for this library anymore now that the new mongo shell (mongosh) can import npm modules natively.
Oh, that's nice!
I'm unsure about your comment regarding PyMongo. This is a Javascript library and you won't be able to use it directly from Python though.
My understanding was that the MongoDB engine was able to execute JavaScript code (using eval
). So I was expecting something like:
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client.test
db.eval("""
const schema = require('mongodb-schema')
schema([{a:1, b:1}, {a: 2}, {a:3, b: 3}])
""")
Similar to https://github.com/variety/variety#an-easy-example
from mongodb-schema.
Oh hi! I didn’t know that this existed.
One small thing to note: mongosh has an experimental feature called snippets, for even more seamlessly integrating external packages into mongosh: https://github.com/mongodb-labs/mongosh-snippets
You can run snippet install analyze-schema
and then call schema()
on collections and cursors: https://github.com/mongodb-labs/mongosh-snippets/tree/main/snippets/analyze-schema
My understanding was that the MongoDB engine was able to execute JavaScript code (using
eval
). So I was expecting something like:
That’s true, but db.eval()
runs code on the server, where require()
is not a thing (because it’s a Node.js/mongosh-specific concept) and mongodb-schema
is almost surely not installed to begin with.
from mongodb-schema.
One small thing to note: mongosh has an experimental feature called snippets, for even more seamlessly integrating external packages into mongosh: mongodb-labs/mongosh-snippets
👍🏻
Neat, I will give it a try.
You can run snippet install analyze-schema and then call schema() on collections and cursors: https://github.com/mongodb-labs/mongosh-snippets/tree/main/snippets/analyze-schema
So snippet install
installs the module client side, right?
That’s true, but db.eval() runs code on the server, where require() is not a thing (because it’s a Node.js/mongosh-specific concept) and mongodb-schema is almost surely not installed to begin with.
Yes but let's say we provide mongodb-schema
as a single file (for instance using https://github.com/vercel/ncc), then it would technically be possible to use mongodb-schema
with db.eval()
?
Out of curiosity, is there a better way than db.eval()
? It seems that this (quite useful) function is deprecated:
- https://docs.mongodb.com/v4.0/reference/command/eval/#dbcmd.eval
- https://pymongo.readthedocs.io/en/stable/api/pymongo/database.html#pymongo.database.Database.eval
Thanks!
from mongodb-schema.
I see, thanks for your reply 🤗
from mongodb-schema.
Related Issues (20)
- Support srv uri host names
- Security issues HOT 7
- What is the Zuul package provided in the package.json as devDependency? HOT 2
- Would you like a pr replacing the travis CI with Github Actions?
- #ampersand: probabilities still wonky
- #ampersand: ObjectID returned as String type HOT 2
- Unknown argument: sample HOT 2
- #ampersand: Date returned as null type
- #ampersand: support for BSON `Long` type HOT 1
- Add the ability to limit the number of array lengths collected
- The command is stuck
- parseSchema is not a function HOT 3
- Add CHANGELOG.md file
- schema parsing throws error if document contains 'toString' as a property path HOT 2
- Runs in cmd, but not in Git Bash... HOT 3
- Abstract it away from mongodb HOT 4
- new optional parameter to limit the lengths collection
- Standard deviation for Array lengths HOT 2
- connect to secondary node with slaveOk=true
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongodb-schema.