These are chat archives for Automattic/mongoose

27th
Apr 2015
Valeri Karpov
@vkarpov15
Apr 27 2015 02:05
@jgornick I don't quite understand what you're asking - can you provide some code examples that show what you're trying to do?
Roland N Sharp
@rolandnsharp
Apr 27 2015 05:41
Hi, I'm creating a soft-delete extension and have overwritten the .remove() static and method. with an .update(something, {deleted: true}

and want to apply the old .remove static and method to .destroy. I'm trying this below, but it is obviously applying the updated softDelete version of .remove().

schema.statics.destroy = function() {
        return this.remove.apply(this, arguments);
    };

Where can I access the original code for .remove()?

Francesco Nero
@francesconero
Apr 27 2015 05:52
@rolandnsharp Sorry for answering with another question, but is it a requirement that you overwrite the remove behaviour? Couldn't you add another soft-remove method?
If yes, there a couple of plugins that already implement that functionality
Roland N Sharp
@rolandnsharp
Apr 27 2015 05:57
@francesconero, Yes I must overwrite the remove behaviour. This is the extension functionality that I require: https://github.com/inlight-media/node-mongoose-soft-delete/blob/master/index.js
The app has no Idea about the concept of softDelete
documents are just deleted and queried, and the results are as if they were deleted
.destroy won't really be used, except in testing environments.
Francesco Nero
@francesconero
Apr 27 2015 06:07
Oh ok, you want to hook into the delete process in a sense
Roland N Sharp
@rolandnsharp
Apr 27 2015 06:07
The idea of the extension, it that it can simply be applied to any application, without modification and work right away.
Francesco Nero
@francesconero
Apr 27 2015 06:08
But wouldn't it cause problems where the application assumes a document doesn't exists in the db anymore, while in fact it still does?
For example resaving a document with a unique field
Roland N Sharp
@rolandnsharp
Apr 27 2015 06:10
Yeah, I'm working on changing some indexing stuff
Otherwise, the application will never deal with deleted documents again.
Francesco Nero
@francesconero
Apr 27 2015 06:12
So you need to save a reference to the old remove?
Roland N Sharp
@rolandnsharp
Apr 27 2015 06:12
yes.
Roland N Sharp
@rolandnsharp
Apr 27 2015 06:24
But I have not been successful in doing this yet. I've been trying to assign it to a global before modifying the .remove() static. destroy = this.remove;
Derick Bailey
@derickbailey
Apr 27 2015 14:03
hi - i'm running into a problem when updating from 4.0.1 to 4.0.2 ... it seems mongoose or one of it's dependencies can't find bson. Error: Cannot find module 'mongodb/node_modules/mongodb-core/node_modules/bson/lib/bson/objectid'
looks like the error is coming from the node-mongodb-native driver
    throw err;
          ^
Error: Cannot find module 'mongodb/node_modules/mongodb-core/node_modules/bson/lib/bson/objectid'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/Users/derickbailey/dev/scott-white/seabass/node_modules/mongoose/lib/drivers/node-mongodb-native/objectid.js:9:16)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
has anyone run in to this? things are working fine w/ 4.0.1 but i get this error on v4.0.2 of mongoose
Adam Ever-Hadani
@adamhadani
Apr 27 2015 16:33
Hey guys, quick question - What's a good way about creating 'fixture' objects for unit-testing with hard coded ids (_id) field - I can't seem to just specify an arbitrary _id: when doing something like Object.create({_id: 'MY_TEST_ID'})
Derick Bailey
@derickbailey
Apr 27 2015 16:35
i do exactly what you suggested and it works
Adam Ever-Hadani
@adamhadani
Apr 27 2015 16:36
@derickbailey you're replying to my question? do you specify a custom '_id' on your model schema with a type? I am not (using mongoose defaults I assume), could that be an issue?
I just got it to work right now after reading some stack overflow post by wrapping the raw string value in mongoose.Types.ObjectId('my_test_id...')
Derick Bailey
@derickbailey
Apr 27 2015 16:37
@adamhadani no, don't specify _id in your schema. mongoose / mongodb handle that for you
@adamhadani i've done that... but usually don't need to. most of the time i just do new MyModel({_id: "553e6593a5d403042f72fa0b"}) and it works
Adam Ever-Hadani
@adamhadani
Apr 27 2015 16:38
@derickbailey - interesting. It works for me now using explicitly the ObjectId('...') syntax, if i remove the objectId wrapper, it throws the following error:
[CastError: Cast to ObjectID failed for value "123456789012" at path "_id"]
Derick Bailey
@derickbailey
Apr 27 2015 16:39
that's not a valid id
@adamhadani the id has to be a certain length... you can generate a new id using mongoose.Types.ObjectId() and then just copy & paste that as a string
@adamhadani which is what i usually do. i have a 3 line js file that i run to generate an id for me when i need one
or... 2 line...
var m = require("mongoose"); console.log(m.Types.ObjectId());
Adam Ever-Hadani
@adamhadani
Apr 27 2015 16:41
@derickbailey - got you, makes sense, I've seen the following error before when trying to use the ObjectId('..') constructor: Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
so I guess - implicit casting requires 24 hex character string, but using the ObjectId('..') constructor syntax allows you also the 12 bytes string format
Derick Bailey
@derickbailey
Apr 27 2015 16:42
@adamhadani seems a reasonable conclusion :)
Adam Ever-Hadani
@adamhadani
Apr 27 2015 16:44
cool, thanks for the help!