Javascript 有没有办法在没有mongodb的情况下创建像_id字符串这样的mongodb?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10593337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:02:45  来源:igfitidea点击:

Is there any way to create mongodb like _id strings without mongodb?

javascriptnode.jsmongodbcouchdbdatabase

提问by fancy

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 生成的 _ids 的格式。主要是因为我可以从客户端提取日期等数据。我打算使用另一个数据库,但仍希望我的文档使用该类型的 _id。如何在不使用 mongodb 的情况下创建这些 ID?

Thanks!

谢谢!

采纳答案by paulmelnikow

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 Node.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

回答by Ruben Stolk

A very easy pseudo ObjectId generator in javascript:

javascript 中一个非常简单的伪 ObjectId 生成器:

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))

回答by dipole_moment

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

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

If you are using javascript with node.

如果您在 node.js 中使用 javascript。

npm install --save bson

npm install --save bson

Using require statement

使用 require 语句

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.

该库还允许您使用良好的旧脚本标签进行导入,但我还没有尝试过。

回答by Grant Carthew

Extending Rubin Stolk's and ChrisV's answer in a more readable syntax (KISS).

以更易读的语法 (KISS) 扩展 Rubin Stolk 和 ChrisV 的答案。

function objectId () {
  return hex(Date.now() / 1000) +
    ' '.repeat(16).replace(/./g, () => hex(Math.random() * 16))
}

function hex (value) {
  return Math.floor(value).toString(16)
}

export default objectId

回答by ChrisV

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

ruben-stolk 的回答很好,但故意不透明?非常容易区分的是:

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!

(实际上字符略少)。不过还是点赞!

回答by deltanovember

There is a detailed specification here

这里有详细的规范

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

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

Which you can use to roll your own id strings

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

回答by ShivaMangina

Here's a link! to a library to do that.

这是一个链接!到图书馆来做到这一点。

https://www.npmjs.com/package/mongo-object-readerYou can read and write hexadecimal strings.

https://www.npmjs.com/package/mongo-object-reader可以读写十六进制字符串。

const { createObjectID, readObjectID,isValidObjectID }  = require('mongo-object-reader');
//Creates a new immutable `ObjectID` instance based on the current system time.
const ObjectID =  createObjectID() //a valid 24 character `ObjectID` hex string.

//returns boolean
// input - a valid 24 character `ObjectID` hex string.
const isValid = isValidObjectID(ObjectID) 

//returns an object with data
// input - a valid 24 character `ObjectID` hex string.
const objectData  = readObjectID(ObjectID) 

console.log(ObjectID) //ObjectID
console.log(isValid)       // true
console.log(objectData)    /*
{ ObjectID: '5e92d4be2ced3f58d92187f5',
  timeStamp:
   { hex: '5e92d4be',
     value: 1586681022,
     createDate: 1970-01-19T08:44:41.022Z },
  random: { hex: '2ced3f58d9', value: 192958912729 },
  incrementValue: { hex: '2187f5', value: 2197493 } }
*/
const { createObjectID, readObjectID,isValidObjectID }  = require('mongo-object-reader');
//Creates a new immutable `ObjectID` instance based on the current system time.
const ObjectID =  createObjectID() //a valid 24 character `ObjectID` hex string.

//returns boolean
// input - a valid 24 character `ObjectID` hex string.
const isValid = isValidObjectID(ObjectID) 

//returns an object with data
// input - a valid 24 character `ObjectID` hex string.
const objectData  = readObjectID(ObjectID) 

console.log(ObjectID) //ObjectID
console.log(isValid)       // true
console.log(objectData)    /*
{ ObjectID: '5e92d4be2ced3f58d92187f5',
  timeStamp:
   { hex: '5e92d4be',
     value: 1586681022,
     createDate: 1970-01-19T08:44:41.022Z },
  random: { hex: '2ced3f58d9', value: 192958912729 },
  incrementValue: { hex: '2187f5', value: 2197493 } }
*/