e-oj / Fawn

Transactions for MongoDB (See the README)
https://www.npmjs.com/package/fawn
MIT License
486 stars 54 forks source link

Accessing elements of subdocument on newly created step 0 document. #36

Closed thedanielfactor closed 6 years ago

thedanielfactor commented 6 years ago

So is I have a schema with a subdocument. I want to create a document with a subdocument and then reference the subdocument with the $ojFuture. So I would like to access the subdocument like:

task.save(GroupSession, groupSession)
    .update("UserPresence", {_id: userId}, {session_id: {$ojFuture: "0._id"}, session_presence_id: {$ojFuture: "0.user_list[0]._id"}})

This does not appear to work. Any ideas on how I could accomplish this?

Here is a more complete example:

var mongoosePaginate = require('mongoose-paginate');
var ttl = require('mongoose-ttl');
var Schema = util.mongoose.Schema;

var SessionListenerSchema = new Schema({
  user_id : String,
  last_keep_alive: Date
});
SessionListenerSchema.plugin(ttl, { ttl: Config.get("SESSION.timeout_seconds")+'s'});

var GroupSessionSchema = new Schema({
  is_all_access: {type: Boolean, default: false},
  is_initiated_by_host: {type: Boolean, default: true},
  host_user_id: String,
  host_state: String,
  ntp_start_time: Number,
  user_list:[SessionListenerSchema],
  call_id: String,
  title: String,
  image_url: String,
  mixer_id: String,
  addoration: { type: Number, default: 0 },
  last_keep_alive: Date
});

GroupSessionSchema.plugin(mongoosePaginate);
GroupSessionSchema.plugin(ttl, { ttl: Config.get("SESSION.timeout_seconds")+'s'});

var GroupSession = util.mongoose.model('GroupSession', GroupSessionSchema);
Promise.promisifyAll(GroupSession);
Promise.promisifyAll(GroupSession.prototype);
module.exports = GroupSession;

I do the following :

var groupSession = new GroupSession();
groupSession.is_all_access = true;
groupSession.user_list = [{user_id:userId, last_keep_alive: Date.now()}];
groupSession.host_user_id = artistId;
groupSession.mixer_id = mixer._id;
groupSession.host_state = 'world';
groupSession.is_initiated_by_host = (mixer.host === userId);

var task = util.fawn.Task();
// Create the session, add both users get updated.
task.save(GroupSession, groupSession)
    .update("UserPresence", {_id: userId}, {session_id: {$ojFuture: "0._id"}, session_presence_id: {$ojFuture: "0.user_list.$._id"}})
     .run({useMongoose: true})
     .then(function (results) {
         var timeoutVal = Date.now() + util.all_access_timeout;
         GroupSession.findOneAndUpdate( {_id: results[0]._id }, {$set: { __ttl: timeoutVal } }).exec()
                    .then(function (session) {
                        resolve(session);
                });
          }).catch(function(error) {
                util.respond(res, 500, {
                    title: 'API Error: No user presence.',
                    message:'There is no presecne for this user.  You must have a presence object.'
                });
            });
e-oj commented 6 years ago

try this

task.save(GroupSession, groupSession)
    .update("UserPresence", {_id: userId}, {
        session_id: {$ojFuture: "0._id"}
       , session_presence_id: {$ojFuture: "0.user_list.0._id"}
    });

The template for accessing an item in a list is "list.index.propertyOfObjectAtIndex"

thedanielfactor commented 6 years ago

Ah, I see what you did there :). Thanks for the fast reply!

Raymond “Daniel" Doran

The Daniel Factor 913.523.6104

On Nov 28, 2017, at 9:59 AM, Emmanuel Olaojo notifications@github.com wrote:

try this

task.save(GroupSession, groupSession) .update("UserPresence", {_id: userId}, { session_id: {$ojFuture: "0._id"} , session_presence_id: {$ojFuture: "0.user_list.0._id"} }); The template for accessing an item in a list is "list.index.propertyOfObjectAtIndex"

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/e-oj/Fawn/issues/36#issuecomment-347570229, or mute the thread https://github.com/notifications/unsubscribe-auth/AAbXcnTLZ1fFKvUMIKimN_IPpFCkYxNRks5s7C3zgaJpZM4Qtdn6.

e-oj commented 6 years ago

No problem 👍