objectid

MongoDB storing arrays of ObjectId's

让人想犯罪 __ 提交于 2019-12-09 05:08:45
问题 In my database I have to store an array of object ids. What should I use? Something like this: [ObjectId("50350e12a36feb1be6000364"), ObjectId("57350e12a37fef1be6000922"), ObjectId("10350e17d34ffb1be6200925")] or something like this: ["50350e12a36feb1be6000364", "57350e12a37fef1be6000922", "10350e17d34ffb1be6200925"] I could save space with the second, and then cast to ObjectId , but am I loosing anything by using this approach? Do ObjectId s behave like foreign keys in relational databases?

Mongoose refuses to cast valid string to ObjectId

纵饮孤独 提交于 2019-12-08 13:17:24
问题 I return the Document _id and use it as a req.params.id to get the document at a later stage. ObjectId.isValid() returns true , then using it in either ObjectId() or Find or FindOne will result in a cast error or hex is not a function. Basically also copy pasted the _id straight from the database to test. I'm using mongoose ^4.7.6 ; _id looks like: 586e30a597f85b69891df304 [Stack trace] [CastError: Cast to ObjectId failed for value "586e30a597f85b69891df304" at path "_id" for model "Module"]

mongodb - Construct DBRef with string or ObjectId

£可爱£侵袭症+ 提交于 2019-12-08 08:41:28
问题 I've noticed that either a string or an object id could be used to construct a DBRef in mongodb. For example db.persons.insert({name: 'alice'}) db.persons.find() // { "_id" : ObjectId("5165419064fada69cef33ea2"), "name" : "alice" } db.persons.insert({name: 'bob', sister: new DBRef('persons', '5165419064fada69cef33ea2')}) // use a string db.persons.find() // { "_id" : ObjectId("5165419064fada69cef33ea2"), "name" : "alice" } // { "_id" : ObjectId("516541c064fada69cef33ea3"), "name" : "bob",

Query Mongoose Schema by ObjectId

会有一股神秘感。 提交于 2019-12-07 15:32:44
问题 Going to need your help again, hopefully for this project, the answer to what I'm having here will be the last. I've seen it's a fairly commonly asked question, but I've tried the tips on another Stack Overflow post, and one by the Google Group, but the solutions haven't worked for me. My code being a little bit like: mongoose = require('mongoose'); Schema = mongoose.Schema; mongoose.connect(MONGO_SERVER); ObjectId = Schema.ObjectId; var RacesSchema = new Schema({ venue_id : { type: mongoose

Shorten ObjectId in node.js and mongoose

坚强是说给别人听的谎言 提交于 2019-12-07 04:37:54
问题 my URLs look like this at the moment: http://www.sitename.com/watch?companyId=507f1f77bcf86cd799439011&employeeId=507f191e810c19729de860ea&someOtherId=..... So, as you can see, it gets pretty long, pretty fast. I was thinking about shortening these ObjectIds. Idea is that I should add new field called "shortId" to every model in my database. So instead of having: var CompanySchema = mongoose.Schema({ /* _id will be added automatically by mongoose */ name: {type: String}, address: {type:

Mongo: how to find by ObjectId that is stored in a subarray?

ε祈祈猫儿з 提交于 2019-12-07 02:32:17
问题 I have a collection with records like this: { "_id" : ObjectId("50ae3bdb50b3d6f01400027a"), "admins": [ObjectId("50ae3bdb50b3d6f014000279"), ObjectId("50ae3bdb50b3d6f01400027e")] } I would like to search by the the 'admin' array. How can I find all documents included for example ObjectId("50ae3bdb50b3d6f014000279") in the sub-array. Thank you. 回答1: You can match against array fields like admins the same as you would a non-array field: db.coll.find({admins: ObjectId("50ae3bdb50b3d6f014000279")

Grails with MongoDB, Object id, and scaffold

旧时模样 提交于 2019-12-06 05:39:15
I have data writing to a mongoDB database with issues using integration tests and the Grails scaffolding. When trying to select a domain instance from the 'list' type page, I get the error "[domain name] not found with id null". I am sure it is because of the Grails url [controller]/[action]/[id]. This id is a string and needs to be converted to an ObjectId for Grails queries. Is there a way to do this so that it affects a specified domain or even better yet, all of the domains at once? I guess as I'm writing my app, I can convert it to an ObjectId from within the action method, but I'd like

mongodb part of objectid most likely to be unique

我的未来我决定 提交于 2019-12-05 20:20:35
In my app I'm letting mongo generate order id's via its ObjectId method. But in user testing we've had some concerns that the order id's are humanly 'intimidating', i.e. if you need to discuss your order with someone over the telephone, reading out 24 alphanumeric characters is a bit tedious. At the same time, I don't really want to have to store two different id's, one 'human-accessible' and one used by mongo internally. So my question is this - is there a way to choose a substring of length 6 or even 8 of the mongo objectId string that I could be fairly sure would be unique ? For example if

Shorten ObjectId in node.js and mongoose

五迷三道 提交于 2019-12-05 10:06:14
my URLs look like this at the moment: http://www.sitename.com/watch?companyId=507f1f77bcf86cd799439011&employeeId=507f191e810c19729de860ea&someOtherId=..... So, as you can see, it gets pretty long, pretty fast. I was thinking about shortening these ObjectIds. Idea is that I should add new field called "shortId" to every model in my database. So instead of having: var CompanySchema = mongoose.Schema({ /* _id will be added automatically by mongoose */ name: {type: String}, address: {type: String}, directorName: {type: String} }); we would have this: var CompanySchema = mongoose.Schema({ /* _id

Generating Mongo ObjectId (_id) with custom time?

放肆的年华 提交于 2019-12-03 21:36:21
问题 I am porting over a table of articles from MySQL to Mongo DB. I understand that the _id field generated by Mongo has the time of creation somehow in there and can be extracted or you can query against it. Because of this, I want to use it to have my created_time INT timestamp from MySQL. Is there a way when moving my data to generate an _id for Mongo that will have the time stamp that I currently have for my records in a separate field? 回答1: Yes, that's possible. In the C# driver for instance