没有mongodb就可以创建像_id字符串一样的mongodb吗?

时间:2022-07-20 15:53:39

I really like the format of the _ids generated by mongodb. Mostly because I can pull data like the date out of them client side. I'm planning to use another database but still want that type of _id for my document. How can I create these ids without using mongodb?

我非常喜欢mongodb生成的_id的格式。主要是因为我可以从客户端提取数据,比如日期。我打算使用另一个数据库,但仍然希望我的文档使用这种类型的_id。如何在不使用mongodb的情况下创建这些id ?

Thanks!

谢谢!

6 个解决方案

#1


18  

Object IDs are usually generated by the client, so any MongoDB driver would have code to generate them.

对象id通常是由客户机生成的,因此任何MongoDB驱动程序都需要代码来生成它们。

If you're looking for JavaScript, here's some code from the MongoDB Node.js driver:

如果您正在寻找JavaScript,下面是MongoDB节点的一些代码。js司机:

https://github.com/mongodb/js-bson/blob/1.0-branch/lib/bson/objectid.js

https://github.com/mongodb/js-bson/blob/1.0-branch/lib/bson/objectid.js

And another, simpler solution:

和另一个更简单的解决方案:

https://github.com/justaprogrammer/ObjectId.js

https://github.com/justaprogrammer/ObjectId.js

#2


22  

A very easy pseudo ObjectId generator in javascript:

一个非常简单的javascript伪目标生成器:

const ObjectId = (m = Math, d = Date, h = 16, s = s => m.floor(s).toString(h)) =>
    s(d.now() / 1000) + ' '.repeat(h).replace(/./g, () => s(m.random() * h))

#3


10  

I have a browser client that generates ObjectIds. I wanted to make sure that I employ the same ObjectId algorithm in the client as the one used in the server. MongoDB has js-bson which can be used to accomplish that.

我有一个生成objective的浏览器客户端。我希望确保在客户机中使用与服务器中使用的相同的objective算法。MongoDB有js-bson可以用来实现这一点。

If you are using javascript with node.

如果您正在使用带有node的javascript。

npm install --save bson

npm安装,节省bson

Using require statement

使用要求语句

var ObjectID = require('bson').ObjectID;

var id  = new ObjectID();
console.log(id.toString());

Using ES6 import statement

使用ES6导入语句

import { ObjectID } from 'bson';

const id  = new ObjectID();
console.log(id.toString());

The library also lets you import using good old script tags but I have not tried this.

这个库还允许您使用优秀的旧脚本标记进行导入,但我还没有尝试过。

#4


1  

ruben-stolk's answer is great, but deliberately opaque? Very slightly easier to pick apart is:

鲁本-斯托克的回答很好,但故意含糊其辞?比较容易区分的是:

const ObjectId = (rnd = r16 => Math.floor(r16).toString(16)) =>
    rnd(Date.now()/1000) + ' '.repeat(16).replace(/./g, () => rnd(Math.random()*16));

(actually in slightly fewer characters). Kudos though!

(实际上是更少的字符)。尽管荣誉!

#5


0  

There is a detailed specification here

这里有一个详细的规范。

http://www.mongodb.org/display/DOCS/Object+IDs

http://www.mongodb.org/display/DOCS/Object + id

Which you can use to roll your own id strings

您可以使用它来滚动自己的id字符串吗

#6


0  

If you are not using arrow functions(es6), below code is the same as ruben's answer of normal function code.

如果您没有使用箭头函数(es6),下面的代码与ruben的正常函数代码的答案是一样的。

function ObjectId () {
  var m = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : Math;
  var d = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Date;
  var h = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 16;
  var s = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : function (s) {
      return m.floor(s).toString(h);
  };
  return s(d.now() / 1000) + ' '.repeat(h).replace(/./g, function () {
      return s(m.random() * h);
  });
}

const ObjectId = ObjectId();

#1


18  

Object IDs are usually generated by the client, so any MongoDB driver would have code to generate them.

对象id通常是由客户机生成的,因此任何MongoDB驱动程序都需要代码来生成它们。

If you're looking for JavaScript, here's some code from the MongoDB Node.js driver:

如果您正在寻找JavaScript,下面是MongoDB节点的一些代码。js司机:

https://github.com/mongodb/js-bson/blob/1.0-branch/lib/bson/objectid.js

https://github.com/mongodb/js-bson/blob/1.0-branch/lib/bson/objectid.js

And another, simpler solution:

和另一个更简单的解决方案:

https://github.com/justaprogrammer/ObjectId.js

https://github.com/justaprogrammer/ObjectId.js

#2


22  

A very easy pseudo ObjectId generator in javascript:

一个非常简单的javascript伪目标生成器:

const ObjectId = (m = Math, d = Date, h = 16, s = s => m.floor(s).toString(h)) =>
    s(d.now() / 1000) + ' '.repeat(h).replace(/./g, () => s(m.random() * h))

#3


10  

I have a browser client that generates ObjectIds. I wanted to make sure that I employ the same ObjectId algorithm in the client as the one used in the server. MongoDB has js-bson which can be used to accomplish that.

我有一个生成objective的浏览器客户端。我希望确保在客户机中使用与服务器中使用的相同的objective算法。MongoDB有js-bson可以用来实现这一点。

If you are using javascript with node.

如果您正在使用带有node的javascript。

npm install --save bson

npm安装,节省bson

Using require statement

使用要求语句

var ObjectID = require('bson').ObjectID;

var id  = new ObjectID();
console.log(id.toString());

Using ES6 import statement

使用ES6导入语句

import { ObjectID } from 'bson';

const id  = new ObjectID();
console.log(id.toString());

The library also lets you import using good old script tags but I have not tried this.

这个库还允许您使用优秀的旧脚本标记进行导入,但我还没有尝试过。

#4


1  

ruben-stolk's answer is great, but deliberately opaque? Very slightly easier to pick apart is:

鲁本-斯托克的回答很好,但故意含糊其辞?比较容易区分的是:

const ObjectId = (rnd = r16 => Math.floor(r16).toString(16)) =>
    rnd(Date.now()/1000) + ' '.repeat(16).replace(/./g, () => rnd(Math.random()*16));

(actually in slightly fewer characters). Kudos though!

(实际上是更少的字符)。尽管荣誉!

#5


0  

There is a detailed specification here

这里有一个详细的规范。

http://www.mongodb.org/display/DOCS/Object+IDs

http://www.mongodb.org/display/DOCS/Object + id

Which you can use to roll your own id strings

您可以使用它来滚动自己的id字符串吗

#6


0  

If you are not using arrow functions(es6), below code is the same as ruben's answer of normal function code.

如果您没有使用箭头函数(es6),下面的代码与ruben的正常函数代码的答案是一样的。

function ObjectId () {
  var m = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : Math;
  var d = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Date;
  var h = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 16;
  var s = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : function (s) {
      return m.floor(s).toString(h);
  };
  return s(d.now() / 1000) + ' '.repeat(h).replace(/./g, function () {
      return s(m.random() * h);
  });
}

const ObjectId = ObjectId();