Handy function for replacing any temporary field/page ids with proper mongo ids.
422791
function reassignConditionalStatementIds(conditionalStatement) {
        conditionalStatement.sourceField = tempFieldPageIdsMap[conditionalStatement.sourceField] || conditionalStatement.sourceField;
        return conditionalStatement;
      }
969331
def update_fields(fields)
      attributes[:id]      = fields['id'] || attributes[:id]
      attributes[:state]   = fields['state'] || attributes[:state]
      attributes[:item_id] = fields['idCheckItem'] || attributes[:item_id]
      self
    end
23563
def ids(data)

      # Just return if nothing to do
      return data if data and data.empty?

      # Support passing id as string
      data = {'_id' => data} if !data or data.is_a?(String)

      # Turn all keys to string
      data = data.stringify_keys

      # Convert id to _id for mongo
      data['_id'] = data.delete('id') if data['id']

      # Convert ids to BSON ObjectId
      data.each do |k, v|
        if v.is_a?(String) and v =~ /^[0-9a-fA-F]{24}$/
          data[k] = oid(v)
        end
      end

      # Return data
      data
    end
1048937
function getMongoHandle(mongoose) {
    var res = {
        mongoose: mongoose,
        modelDocs: {},
        modelESchemas: {},
        mongoMaps: {}
    };
    var modelES = Schemaload.getExtendedSchemaModel(mongoose);
    return modelES.distinct('modelname').then((modelnames) => {
        debuglog(() => 'here distinct modelnames ' + JSON.stringify(modelnames));
        return Promise.all(modelnames.map(function (modelname) {
            debuglog(() => 'creating tripel for ' + modelname);
            return Promise.all([Schemaload.getExtendSchemaDocFromDB(mongoose, modelname),
                Schemaload.makeModelFromDB(mongoose, modelname),
                Schemaload.getModelDocFromDB(mongoose, modelname)]).then((value) => {
                debuglog(() => 'attempting to load ' + modelname + 'to create mongomap');
                var [extendedSchema, model, modelDoc] = value;
                res.modelESchemas[modelname] = extendedSchema;
                res.modelDocs[modelname] = modelDoc;
                res.mongoMaps[modelname] = MongoMap.makeMongoMap(modelDoc, extendedSchema);
                debuglog(() => 'created mongomap for ' + modelname);
            });
        }));
    }).then(() => {
        return res;
    });
    //var modelDoc = Schemaload.getExtendedDocModel(mongoose);
    //res.modelDocs[ISchema.MongoNLQ.MODELNAME_METAMODELS] = modelDoc;
    //return Promise.resolve(res);
}
1100524
function (id, field, values) {
            var self = this;

            var query = { _id: waterline.nodes.mongo.objectId(id)};
            query[field] = {$exists : false};

            var update = { $set: {} };
            update.$set[field] = values;

            var options = {new: true};
            return self.findAndModifyMongo(query, {}, update, options);
        }
1340733
function(attrs) {
      // Check for changes of magic fields.
      var model = this;
      var specialFields = ["id", "objectId", "createdAt", "updatedAt"];
      Parse._arrayEach(specialFields, function(attr) {
        if (attrs[attr]) {
          if (attr === "objectId") {
            model.id = attrs[attr];
          } else if ((attr === "createdAt" || attr === "updatedAt") &&
                     !_.isDate(attrs[attr])) {
            model[attr] = Parse._parseDate(attrs[attr]);
          } else {
            model[attr] = attrs[attr];
          }
          delete attrs[attr];
        }
      });
    }
980753
def apply_update(self, doc, update_spec):
        """"""
        # Replace a whole document
        if not '$set' in update_spec and not '$unset' in update_spec:
            # update_spec contains the new document.
            # Update the key in Solr based on the unique_key mentioned as
            # parameter.
            update_spec['_id'] = doc[self.unique_key]
            return update_spec
        for to_set in update_spec.get("$set", []):
            value = update_spec['$set'][to_set]
            # Find dotted-path to the value, remove that key from doc, then
            # put value at key:
            keys_to_pop = []
            for key in doc:
                if key.startswith(to_set):
                    if key == to_set or key[len(to_set)] == '.':
                        keys_to_pop.append(key)
            for key in keys_to_pop:
                doc.pop(key)
            doc[to_set] = value
        for to_unset in update_spec.get("$unset", []):
            # MongoDB < 2.5.2 reports $unset for fields that don't exist within
            # the document being updated.
            keys_to_pop = []
            for key in doc:
                if key.startswith(to_unset):
                    if key == to_unset or key[len(to_unset)] == '.':
                        keys_to_pop.append(key)
            for key in keys_to_pop:
                doc.pop(key)
        return doc
1127516
def params_to_mongo(query_params):
    
    if not query_params:
        return {}
    for key, value in query_params.items():
        if isinstance(value, list):
            query_params[key] = {'$in': value}
    return query_params
1192656
def _replace_placeholder(self, spider):
        
        return self.db_name % {'spider': spider.name}, self.collection_name % {'spider': spider.name}
1350362
def clean(self):
        """"""
        doc = self._resource
        result = {k: v for k, v in doc.iteritems() if k not in
                  self.internal_fields}

        if '_id' in doc and 'id' not in result:
            result['id'] = doc['_id']

        return result
1240707
def prepare_data(self, data=None):
        '''
        
        '''
        data = data or self.export_data(native=True)
        # make sure we don't persist a null _id and let MongoDB auto-generate it
        if '_id' in data and data['_id'] is None:
            del data['_id']
        return data
435198
def set_all_revisions_to_in_process(self, ids):
        

        predicate = {
            "_id" : {
                "$in" : [ ObjectId(id) for id in ids ]
            }
        }

        set = {"$set": { "inProcess": True }}

        yield self.revisions.collection.update(predicate, set, multi=True)
258218
def convert_ids_to_object_ids(obj_map):
    """"""
    converted_map = {}
    for key, value in obj_map.items():
        if key == '_id':
            # hacky, but using alias sends back the whole ID string, like
            #   assessment.Item%3A5758326b4a40452d6eee1fa1%40ODL.MIT.EDU
            # so we have to preserve it
            try:
                converted_map[key] = ObjectId(value)
            except InvalidId:
                converted_map[key] = value
        elif isinstance(value, dict):
            converted_map[key] = convert_ids_to_object_ids(value)
        elif isinstance(value, list):
            new_list = []
            for internal_item in value:
                if isinstance(internal_item, dict):
                    new_list.append(convert_ids_to_object_ids(internal_item))
                else:
                    new_list.append(internal_item)
            converted_map[key] = new_list
        else:
            converted_map[key] = value
    return converted_map
501808
function getDocsToPersist(docId, view, docIdsToChangesAndEmits) {
  var metaDocId = '_local/doc_' + docId;
  var defaultMetaDoc = {_id: metaDocId, keys: []};
  var docData = docIdsToChangesAndEmits[docId];
  var indexableKeysToKeyValues = docData.indexableKeysToKeyValues;
  var changes = docData.changes;

  function getMetaDoc() {
    if (isGenOne(changes)) {
      // generation 1, so we can safely assume initial state
      // for performance reasons (avoids unnecessary GETs)
      return Promise.resolve(defaultMetaDoc);
    }
    return view.db.get(metaDocId).catch(defaultsTo(defaultMetaDoc));
  }

  function getKeyValueDocs(metaDoc) {
    if (!metaDoc.keys.length) {
      // no keys, no need for a lookup
      return Promise.resolve({rows: []});
    }
    return view.db.allDocs({
      keys: metaDoc.keys,
      include_docs: true
    });
  }

  function processKvDocs(metaDoc, kvDocsRes) {
    var kvDocs = [];
    var oldKeysMap = {};

    for (var i = 0, len = kvDocsRes.rows.length; i < len; i++) {
      var row = kvDocsRes.rows[i];
      var doc = row.doc;
      if (!doc) { // deleted
        continue;
      }
      kvDocs.push(doc);
      oldKeysMap[doc._id] = true;
      doc._deleted = !indexableKeysToKeyValues[doc._id];
      if (!doc._deleted) {
        var keyValue = indexableKeysToKeyValues[doc._id];
        if ('value' in keyValue) {
          doc.value = keyValue.value;
        }
      }
    }

    var newKeys = Object.keys(indexableKeysToKeyValues);
    newKeys.forEach(function (key) {
      if (!oldKeysMap[key]) {
        // new doc
        var kvDoc = {
          _id: key
        };
        var keyValue = indexableKeysToKeyValues[key];
        if ('value' in keyValue) {
          kvDoc.value = keyValue.value;
        }
        kvDocs.push(kvDoc);
      }
    });
    metaDoc.keys = uniq(newKeys.concat(metaDoc.keys));
    kvDocs.push(metaDoc);

    return kvDocs;
  }

  return getMetaDoc().then(function (metaDoc) {
    return getKeyValueDocs(metaDoc).then(function (kvDocsRes) {
      return processKvDocs(metaDoc, kvDocsRes);
    });
  });
}
1310989
def mget(self, ids, index=None, doc_type=None, **query_params):
        
        if not ids:
            return []

        body = []
        for value in ids:
            if isinstance(value, tuple):
                if len(value) == 3:
                    a, b, c = value
                    body.append({"_index": a,
                                 "_type": b,
                                 "_id": c})
                elif len(value) == 4:
                    a, b, c, d = value
                    body.append({"_index": a,
                                 "_type": b,
                                 "_id": c,
                                 "fields": d})

            else:
                if index is None:
                    raise InvalidQuery("index value is required for id")
                if doc_type is None:
                    raise InvalidQuery("doc_type value is required for id")
                body.append({"_index": index,
                             "_type": doc_type,
                             "_id": value})

        results = self._send_request('GET', "/_mget", body={'docs': body},
                                     params=query_params)
        if 'docs' in results:
            model = self.model
            return [model(self, item) for item in results['docs']]
        return []
266210
def create_id2collection_info(path, tag):
    
    d = {}
    for triple in os.walk(path):
        root, files = triple[0], triple[2]
        for filename in files:
            if filename.endswith('.json'):
                # trim file extension and prepend owner_id (from path)
                collection_id = "{u}/{n}".format(u=root.split('/')[-1], n=filename[:-5])
                d[collection_id] = (tag, root, os.path.join(root, filename))
    return d
694004
def update(self, **kwargs):
        
        if self.is_valid:
            if '_id' in self._document:
                to_update = self.find_one({'_id': self._id})

                if to_update:
                    before = self.before_update(old=to_update)
                    if before:
                        return before

                    try:
                        self.replace_one({'_id': self._id}, self._document)

                        self.after_update(old=to_update)

                        return self._document
                    except PyMongoException as exc:
                        return PyMongoError(
                            error_message=exc.details.get(
                                'errmsg', exc.details.get(
                                    'err', 'PyMongoError.'
                                )
                            ),
                            operation='update', collection=type(self).__name__,
                            document=self._document,
                        )
                else:
                    return DocumentNotFoundError(type(self).__name__, self._id)
            else:
                return UnidentifiedDocumentError(
                    type(self).__name__, self._document
                )

        return self._errors