'Internal insert helper.'

def _insert(self, sock_info, docs, ordered=True, check_keys=True, manipulate=False, write_concern=None, op_id=None, bypass_doc_val=False):

    if isinstance(docs, collections.Mapping):
        return self._insert_one(sock_info, docs, ordered, check_keys, manipulate, write_concern, op_id, bypass_doc_val)
    ids = []
    if manipulate:
        def gen():
            'Generator    that    applies    SON    manipulators    to    each    document\n                                                                and    adds    _id    if    necessary.\n                                                                '
            _db = self.__database
            for doc in docs:
                doc = _db._apply_incoming_manipulators(doc, self)
                if (not (isinstance(doc, RawBSONDocument) or ('_id' in doc))):
                    doc['_id'] = ObjectId()
                doc = _db._apply_incoming_copying_manipulators(doc, self)
                ids.append(doc['_id'])
                (yield doc)
    else:
        def gen():
            'Generator    that    only    tracks    existing    _ids.'
            for doc in docs:
                if (not isinstance(doc, RawBSONDocument)):
                    ids.append(doc.get('_id'))
                (yield doc)
    concern = (write_concern or self.write_concern).document
    acknowledged = (concern.get('w') != 0)
    command = SON([('insert', self.name), ('ordered', ordered)])
    if concern:
        command['writeConcern'] = concern
    if (op_id is None):
        op_id = message._randint()
    if (bypass_doc_val and (sock_info.max_wire_version >= 4)):
        command['bypassDocumentValidation'] = True
    bwc = message._BulkWriteContext(self.database.name, command, sock_info, op_id, self.database.client._event_listeners)
    if ((sock_info.max_wire_version > 1) and acknowledged):
        results = message._do_batched_write_command((self.database.name + '.$cmd'), message._INSERT, command, gen(), check_keys, self.__write_response_codec_options, bwc)
        _check_write_command_response(results)
    else:
        message._do_batched_insert(self.__full_name, gen(), check_keys, acknowledged, concern, (not ordered), self.__write_response_codec_options, bwc)
    return ids
