def query(options, collection_name, num_to_skip, num_to_return, query, field_selector=None, uuid_subtype=OLD_UUID_SUBTYPE, codec_options=None): """Get a **query** message. """ if codec_options is not None: uuid_subtype = codec_options.uuid_representation data = struct.pack("<I", options) data += bson._make_c_string(collection_name) data += struct.pack("<i", num_to_skip) data += struct.pack("<i", num_to_return) encoded = bson.BSON.encode(query, False, uuid_subtype) data += encoded max_bson_size = len(encoded) if field_selector is not None: encoded = bson.BSON.encode(field_selector, False, uuid_subtype) data += encoded max_bson_size = max(len(encoded), max_bson_size) (request_id, query_message) = __pack_message(2004, data) return (request_id, query_message, max_bson_size)
def update( collection_name, upsert, multi, spec, doc, safe, last_error_args, check_keys, uuid_subtype, codec_options=None ): """Get an **update** message. """ if codec_options is not None: uuid_subtype = codec_options.uuid_representation options = 0 if upsert: options += 1 if multi: options += 2 data = _ZERO_32 data += bson._make_c_string(collection_name) data += struct.pack("<i", options) data += bson.BSON.encode(spec, False, uuid_subtype) encoded = bson.BSON.encode(doc, check_keys, uuid_subtype) data += encoded if safe: (_, update_message) = __pack_message(2001, data) (request_id, error_message, _) = __last_error(collection_name, last_error_args) return (request_id, update_message + error_message, len(encoded)) else: (request_id, update_message) = __pack_message(2001, data) return (request_id, update_message, len(encoded))
def insert(collection_name, docs, check_keys, safe, last_error_args, continue_on_error, opts): """Get an **insert** message. Used by the Bulk API to insert into pre-2.6 servers. Collection.insert uses _do_batched_insert. """ options = 0 if continue_on_error: options += 1 data = struct.pack("<i", options) data += bson._make_c_string(collection_name) encoded = [bson.BSON.encode(doc, check_keys, opts) for doc in docs] if not encoded: raise InvalidOperation("cannot do an empty bulk insert") max_bson_size = max(map(len, encoded)) data += _EMPTY.join(encoded) if safe: (_, insert_message) = __pack_message(2002, data) (request_id, error_message, _) = __last_error(collection_name, last_error_args) return (request_id, insert_message + error_message, max_bson_size) else: (request_id, insert_message) = __pack_message(2002, data) return (request_id, insert_message, max_bson_size)
def _do_batched_insert(collection_name, docs, check_keys, safe, last_error_args, continue_on_error, uuid_subtype, client): """Insert `docs` using multiple batches. """ def _insert_message(insert_message, send_safe): """Build the insert message with header and GLE. """ request_id, final_message = __pack_message(2002, insert_message) if send_safe: request_id, error_message, _ = __last_error(collection_name, last_error_args) final_message += error_message return request_id, final_message send_safe = safe or not continue_on_error last_error = None begin = struct.pack("<i", int(continue_on_error)) begin += bson._make_c_string(collection_name) message_length = len(begin) data = [begin] has_docs = False for doc in docs: encoded = bson.BSON.encode(doc, check_keys, uuid_subtype) encoded_length = len(encoded) too_large = (encoded_length > client.max_bson_size) message_length += encoded_length if message_length < client.max_message_size and not too_large: data.append(encoded) has_docs = True continue if has_docs: # We have enough data, send this message. try: client._send_message(_insert_message(_EMPTY.join(data), send_safe), send_safe) # Exception type could be OperationFailure or a subtype # (e.g. DuplicateKeyError) except OperationFailure, exc: # Like it says, continue on error... if continue_on_error: # Store exception details to re-raise after the final batch. last_error = exc # With unacknowledged writes just return at the first error. elif not safe: return # With acknowledged writes raise immediately. else: raise if too_large: raise DocumentTooLarge("BSON document too large (%d bytes)" " - the connected server supports" " BSON document sizes up to %d" " bytes." % (encoded_length, client.max_bson_size)) message_length = len(begin) + encoded_length data = [begin, encoded]
def insert(collection_name, docs, check_keys, safe, last_error_args, continue_on_error, uuid_subtype): """Get an **insert** message. .. note:: As of PyMongo 2.6, this function is no longer used. It is being kept (with tests) for backwards compatibility with 3rd party libraries that may currently be using it, but will likely be removed in a future release. """ options = 0 if continue_on_error: options += 1 data = struct.pack("<i", options) data += bson._make_c_string(collection_name) encoded = [bson.BSON.encode(doc, check_keys, uuid_subtype) for doc in docs] if not encoded: raise InvalidOperation("cannot do an empty bulk insert") max_bson_size = max(map(len, encoded)) data += _EMPTY.join(encoded) if safe: (_, insert_message) = __pack_message(2002, data) (request_id, error_message, _) = __last_error(collection_name, last_error_args) return (request_id, insert_message + error_message, max_bson_size) else: (request_id, insert_message) = __pack_message(2002, data) return (request_id, insert_message, max_bson_size)
def query(options, collection_name, num_to_skip, num_to_return, query, field_selector, opts, check_keys=False): """Get a **query** message. """ data = struct.pack("<I", options) data += bson._make_c_string(collection_name) data += struct.pack("<i", num_to_skip) data += struct.pack("<i", num_to_return) if check_keys: # Temporarily remove $clusterTime to avoid an error from the $-prefix. cluster_time = query.pop('$clusterTime', None) encoded = bson.BSON.encode(query, True, opts) if cluster_time is not None: extra = bson._name_value_to_bson(b"$clusterTime\x00", cluster_time, False, opts) encoded = (bson._PACK_INT(len(encoded) + len(extra)) + encoded[4:-1] + extra + b'\x00') query['$clusterTime'] = cluster_time else: encoded = bson.BSON.encode(query, False, opts) data += encoded max_bson_size = len(encoded) if field_selector is not None: encoded = bson.BSON.encode(field_selector, False, opts) data += encoded max_bson_size = max(len(encoded), max_bson_size) (request_id, query_message) = __pack_message(2004, data) return (request_id, query_message, max_bson_size)
def _op_msg_no_header(flags, command, identifier, docs, opts): """Get a OP_MSG message. Note: this method handles multiple documents in a type one payload but it does not perform batch splitting and the total message size is only checked *after* generating the entire message. """ # Encode the command document in payload 0 without checking keys. encoded = _dict_to_bson(command, False, opts) flags_type = _pack_op_msg_flags_type(flags, 0) total_size = len(encoded) max_doc_size = 0 if identifier: type_one = _pack_byte(1) cstring = _make_c_string(identifier) encoded_docs = [_dict_to_bson(doc, False, opts) for doc in docs] size = len(cstring) + sum(len(doc) for doc in encoded_docs) + 4 encoded_size = _pack_int(size) total_size += size max_doc_size = max(len(doc) for doc in encoded_docs) data = [flags_type, encoded, type_one, encoded_size, cstring ] + encoded_docs else: data = [flags_type, encoded] return b"".join(data), total_size, max_doc_size
def query(options, collection_name, num_to_skip, num_to_return, query, field_selector, opts, check_keys=False): """Get a **query** message. """ data = struct.pack("<I", options) data += bson._make_c_string(collection_name) data += struct.pack("<i", num_to_skip) data += struct.pack("<i", num_to_return) if check_keys: # Temporarily remove $clusterTime to avoid an error from the $-prefix. cluster_time = query.pop('$clusterTime', None) encoded = bson.BSON.encode(query, True, opts) if cluster_time is not None: extra = bson._name_value_to_bson( b"$clusterTime\x00", cluster_time, False, opts) encoded = ( bson._PACK_INT(len(encoded) + len(extra)) + encoded[4:-1] + extra + b'\x00') query['$clusterTime'] = cluster_time else: encoded = bson.BSON.encode(query, False, opts) data += encoded max_bson_size = len(encoded) if field_selector is not None: encoded = bson.BSON.encode(field_selector, False, opts) data += encoded max_bson_size = max(len(encoded), max_bson_size) (request_id, query_message) = __pack_message(2004, data) return (request_id, query_message, max_bson_size)
def _query(options, collection_name, num_to_skip, num_to_return, query, field_selector, opts, check_keys): """Get an OP_QUERY message.""" encode = _dict_to_bson # Make local. Uses extensions. if check_keys and "$clusterTime" in query: # Temporarily remove $clusterTime to avoid an error from the $-prefix. cluster_time = query.pop('$clusterTime') encoded = encode(query, True, opts) extra = bson._name_value_to_bson( b"$clusterTime\x00", cluster_time, False, opts) encoded = ( _pack_int(len(encoded) + len(extra)) + encoded[4:-1] + extra + b'\x00') query['$clusterTime'] = cluster_time else: encoded = encode(query, check_keys, opts) if field_selector: efs = encode(field_selector, False, opts) else: efs = b"" max_bson_size = max(len(encoded), len(efs)) return b"".join([ _pack_int(options), _make_c_string(collection_name), _pack_int(num_to_skip), _pack_int(num_to_return), encoded, efs]), max_bson_size
def _get_more(collection_name, num_to_return, cursor_id): """Get an OP_GET_MORE message.""" return b"".join([ _ZERO_32, _make_c_string(collection_name), _pack_int(num_to_return), _pack_long_long(cursor_id)])
def sendMessage(self, operation, collection, message, query_opts=_ZERO): #print "sending %d to %s" % (operation, self) fullname = collection and bson._make_c_string(collection) or "" message = query_opts + fullname + message header = struct.pack("<iiii", 16 + len(message), self.__id, 0, operation) self.transport.write(header + message) self.__id += 1
def insert(collection_name, docs, check_keys, safe, last_error_args, continue_on_error, uuid_subtype): """Get an **insert** message. .. note:: As of PyMongo 2.6, this function is no longer used. It is being kept (with tests) for backwards compatibility with 3rd party libraries that may currently be using it, but will likely be removed in a future release. """ options = 0 if continue_on_error: options += 1 data = struct.pack("<i", options) data += bson._make_c_string(collection_name) encoded = [bson.BSON.encode(doc, check_keys, uuid_subtype) for doc in docs] if not encoded: raise InvalidOperation("cannot do an empty bulk insert") max_bson_size = max(list(map(len, encoded))) data += _EMPTY.join(encoded) if safe: (_, insert_message) = __pack_message(2002, data) (request_id, error_message, _) = __last_error(collection_name, last_error_args) return (request_id, insert_message + error_message, max_bson_size) else: (request_id, insert_message) = __pack_message(2002, data) return (request_id, insert_message, max_bson_size)
def mongo_gen_update(collection, spec, doc, upsert=False, multi=False): return mongo_addhdr(2001, __binzero + bson._make_c_string(collection) + struct.pack('<i', (upsert and 1 or 0)|(multi and 2 or 0) ) + bson.BSON.encode(spec) + bson.BSON.encode(doc) )[1]
def query( options, collection_name, num_to_skip, num_to_return, query, field_selector=None, uuid_subtype=OLD_UUID_SUBTYPE, codec_options=None, ): """Get a **query** message. """ if codec_options is not None: uuid_subtype = codec_options.uuid_representation data = struct.pack("<I", options) data += bson._make_c_string(collection_name) data += struct.pack("<i", num_to_skip) data += struct.pack("<i", num_to_return) encoded = bson.BSON.encode(query, False, uuid_subtype) data += encoded max_bson_size = len(encoded) if field_selector is not None: encoded = bson.BSON.encode(field_selector, False, uuid_subtype) data += encoded max_bson_size = max(len(encoded), max_bson_size) (request_id, query_message) = __pack_message(2004, data) return (request_id, query_message, max_bson_size)
def update(collection_name, upsert, multi, spec, doc, safe, last_error_args, check_keys, uuid_subtype, codec_options=None): """Get an **update** message. """ if codec_options is not None: uuid_subtype = codec_options.uuid_representation options = 0 if upsert: options += 1 if multi: options += 2 data = _ZERO_32 data += bson._make_c_string(collection_name) data += struct.pack("<i", options) data += bson.BSON.encode(spec, False, uuid_subtype) encoded = bson.BSON.encode(doc, check_keys, uuid_subtype) data += encoded if safe: (_, update_message) = __pack_message(2001, data) (request_id, error_message, _) = __last_error(collection_name, last_error_args) return (request_id, update_message + error_message, len(encoded)) else: (request_id, update_message) = __pack_message(2001, data) return (request_id, update_message, len(encoded))
def mongo_gen_query(options, collection, skip, num, query, fields=None): return mongo_addhdr(2004, struct.pack('<I', options) + bson._make_c_string(collection) + struct.pack("<ii", skip, num) + bson.BSON.encode(query) + (fields and bson.BSON.encode(fields) or '') )
def _delete(collection_name, spec, opts, flags): """Get an OP_DELETE message.""" encoded = _dict_to_bson(spec, False, opts) # Uses extensions. return b"".join([ _ZERO_32, _make_c_string(collection_name), _pack_int(flags), encoded]), len(encoded)
def _insert(collection_name, docs, check_keys, flags, opts): """Get an OP_INSERT message""" encode = _dict_to_bson # Make local. Uses extensions. if len(docs) == 1: encoded = encode(docs[0], check_keys, opts) return b"".join([ b"\x00\x00\x00\x00", # Flags don't matter for one doc. _make_c_string(collection_name), encoded]), len(encoded) encoded = [encode(doc, check_keys, opts) for doc in docs] if not encoded: raise InvalidOperation("cannot do an empty bulk insert") return b"".join([ _pack_int(flags), _make_c_string(collection_name), b"".join(encoded)]), max(map(len, encoded))
def _send_message(self, operation, data): """Wrap up a message and send it. """ # reserved int, full collection name, message data message = _ZERO message += bson._make_c_string(self.full_name()) message += data return self.__database.connection()._send_message(operation, message)
def get_more(collection_name, num_to_return, cursor_id): """Get a **getMore** message. """ data = _ZERO_32 data += bson._make_c_string(collection_name) data += struct.pack("<i", num_to_return) data += struct.pack("<q", cursor_id) return __pack_message(2005, data)
def insert(col, doc_or_docs): try: doc_or_docs.fromkeys doc_or_docs = [doc_or_docs] except AttributeError: pass doc_data = "".join(BSON.encode(doc) for doc in doc_or_docs) colname = _make_c_string(col) return "%s%s%s" % (_ZERO, colname, doc_data)
def _do_batched_insert(collection_name, docs, check_keys, safe, last_error_args, continue_on_error, uuid_subtype, client): """Insert `docs` using multiple batches. """ def _insert_message(insert_message, send_safe): """Build the insert message with header and GLE. """ request_id, final_message = __pack_message(2002, insert_message) if send_safe: request_id, error_message, _ = __last_error( collection_name, last_error_args) final_message += error_message return request_id, final_message if not docs: raise InvalidOperation("cannot do an empty bulk insert") last_error = None begin = struct.pack("<i", int(continue_on_error)) begin += bson._make_c_string(collection_name) message_length = len(begin) data = [begin] for doc in docs: encoded = bson.BSON.encode(doc, check_keys, uuid_subtype) encoded_length = len(encoded) if encoded_length > client.max_bson_size: raise InvalidDocument("BSON document too large (%d bytes)" " - the connected server supports" " BSON document sizes up to %d" " bytes." % (encoded_length, client.max_bson_size)) message_length += encoded_length if message_length < client.max_message_size: data.append(encoded) continue # We have enough data, send this message. send_safe = safe or not continue_on_error try: client._send_message(_insert_message(EMPTY.join(data), send_safe), send_safe) # Exception type could be OperationFailure or a subtype # (e.g. DuplicateKeyError) except OperationFailure, exc: # Like it says, continue on error... if continue_on_error: # Store exception details to re-raise after the final batch. last_error = exc # With unacknowledged writes just return at the first error. elif not safe: return # With acknowledged writes raise immediately. else: raise message_length = len(begin) + encoded_length data = [begin, encoded]
def update(col, spec, doc, upsert, multi): colname = _make_c_string(col) flags = 0 if upsert: flags |= 1 << 0 if multi: flags |= 1 << 1 fmt = '<i%dsi' % len(colname) part = struct.pack(fmt, 0, colname, flags) return "%s%s%s" % (part, BSON.encode(spec), BSON.encode(doc))
def query(col, spec, fields, skip, limit): data = [ _ZERO, _make_c_string(col), struct.pack('<ii', skip, limit), BSON.encode(spec or {}), ] if fields: data.append(BSON.encode(dict.fromkeys(fields, 1))) return "".join(data)
def query(options, collection_name, num_to_skip, num_to_return, query, field_selector=None): """Get a **query** message. """ data = struct.pack("<I", options) data += bson._make_c_string(collection_name) data += struct.pack("<i", num_to_skip) data += struct.pack("<i", num_to_return) data += bson.BSON.encode(query) if field_selector is not None: data += bson.BSON.encode(field_selector) return __pack_message(2004, data)
def insert(collection_name, docs, check_keys, safe): """Get an **insert** message. """ data = __ZERO data += bson._make_c_string(collection_name) data += "".join([bson.BSON.from_dict(doc, check_keys) for doc in docs]) if safe: (_, insert_message) = __pack_message(2002, data) (request_id, error_message) = __last_error() return (request_id, insert_message + error_message) else: return __pack_message(2002, data)
def delete(collection_name, spec, safe): """Get a **delete** message. """ data = __ZERO data += bson._make_c_string(collection_name) data += __ZERO data += bson.BSON.from_dict(spec) if safe: (_, remove_message) = __pack_message(2006, data) (request_id, error_message) = __last_error() return (request_id, remove_message + error_message) else: return __pack_message(2006, data)
def delete(collection_name, spec, safe, last_error_args): """Get a **delete** message. """ data = __ZERO data += bson._make_c_string(collection_name) data += __ZERO encoded = bson.BSON.encode(spec) data += encoded if safe: (_, remove_message) = __pack_message(2006, data) (request_id, error_message, _) = __last_error(last_error_args) return (request_id, remove_message + error_message, len(encoded)) else: (request_id, remove_message) = __pack_message(2006, data) return (request_id, remove_message, len(encoded))
def delete(collection_name, spec, safe, last_error_args, uuid_subtype, options=0): """Get a **delete** message. """ data = _ZERO_32 data += bson._make_c_string(collection_name) data += struct.pack("<I", options) encoded = bson.BSON.encode(spec, False, uuid_subtype) data += encoded if safe: (_, remove_message) = __pack_message(2006, data) (request_id, error_message, _) = __last_error(collection_name, last_error_args) return (request_id, remove_message + error_message, len(encoded)) else: (request_id, remove_message) = __pack_message(2006, data) return (request_id, remove_message, len(encoded))
def insert(collection_name, docs, check_keys, safe, last_error_args): """Get an **insert** message. """ data = __ZERO data += bson._make_c_string(collection_name) bson_data = "".join([bson.BSON.encode(doc, check_keys) for doc in docs]) if not bson_data: raise InvalidOperation("cannot do an empty bulk insert") data += bson_data if safe: (_, insert_message) = __pack_message(2002, data) (request_id, error_message) = __last_error(last_error_args) return (request_id, insert_message + error_message) else: return __pack_message(2002, data)
def _update(collection_name, upsert, multi, spec, doc, check_keys, opts): """Get an OP_UPDATE message.""" flags = 0 if upsert: flags += 1 if multi: flags += 2 encode = _dict_to_bson # Make local. Uses extensions. encoded_update = encode(doc, check_keys, opts) return b"".join([ _ZERO_32, _make_c_string(collection_name), _pack_int(flags), encode(spec, False, opts), encoded_update]), len(encoded_update)
def _query_impl(options, collection_name, num_to_skip, num_to_return, query, field_selector, opts, check_keys): """Get an OP_QUERY message.""" encoded = _dict_to_bson(query, check_keys, opts) if field_selector: efs = _dict_to_bson(field_selector, False, opts) else: efs = b"" max_bson_size = max(len(encoded), len(efs)) return b"".join([ _pack_int(options), _make_c_string(collection_name), _pack_int(num_to_skip), _pack_int(num_to_return), encoded, efs ]), max_bson_size
def insert(collection_name, docs, check_keys, safe, last_error_args): """Get an **insert** message. """ data = __ZERO data += bson._make_c_string(collection_name) bson_data = "".join([bson.BSON.encode(doc, check_keys) for doc in docs]) if not bson_data: raise InvalidOperationError("cannot do an empty bulk insert") data += bson_data if safe: (_, insert_message) = __pack_message(2002, data) (request_id, error_message) = __last_error(last_error_args) return (request_id, insert_message + error_message) else: return __pack_message(2002, data)
def delete(collection_name, spec, safe, last_error_args, uuid_subtype): """Get a **delete** message. """ data = __ZERO data += bson._make_c_string(collection_name) data += __ZERO encoded = bson.BSON.encode(spec, False, uuid_subtype) data += encoded if safe: (_, remove_message) = __pack_message(2006, data) (request_id, error_message, _) = __last_error(last_error_args) return (request_id, remove_message + error_message, len(encoded)) else: (request_id, remove_message) = __pack_message(2006, data) return (request_id, remove_message, len(encoded))
def _msg_build_insert(collection_name, docs, check_keys=True): """Get an **insert** message. """ max_bson_size = 0 data = __ZERO if isinstance(docs, dict): docs = [docs] data += bson._make_c_string(collection_name) encoded = [bson.BSON.encode(doc, check_keys) for doc in docs] if not encoded: raise Exception("cannot do an empty bulk insert") max_bson_size = max(map(len, encoded)) data += "".join(encoded) (request_id, insert_message) = __pack_message(2002, data) return (request_id, insert_message, max_bson_size)
def query(options, collection_name, num_to_skip, num_to_return, query, field_selector, opts, check_keys=False): """Get a **query** message. """ data = struct.pack("<I", options) data += bson._make_c_string(collection_name) data += struct.pack("<i", num_to_skip) data += struct.pack("<i", num_to_return) encoded = bson.BSON.encode(query, check_keys, opts) data += encoded max_bson_size = len(encoded) if field_selector is not None: encoded = bson.BSON.encode(field_selector, False, opts) data += encoded max_bson_size = max(len(encoded), max_bson_size) (request_id, query_message) = __pack_message(2004, data) return (request_id, query_message, max_bson_size)
def query(options, collection_name, num_to_skip, num_to_return, query, field_selector=None): """Get a **query** message. """ data = struct.pack("<I", options) data += bson._make_c_string(collection_name) data += struct.pack("<i", num_to_skip) data += struct.pack("<i", num_to_return) data += bson.BSON.from_dict(query) if field_selector is not None: data += bson.BSON.from_dict(field_selector) return __pack_message(2004, data)
def delete(collection_name, spec, opts, flags=0): """Get an unacknowledged **delete** message. `opts` is a CodecOptions. `flags` is a bit vector that may contain the SingleRemove flag or not: http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-delete .. versionchanged:: 3.6 Removed 'safe' and 'last_error_args' arguments. """ data = _ZERO_32 data += bson._make_c_string(collection_name) data += struct.pack("<I", flags) encoded = bson.BSON.encode(spec, False, opts) data += encoded request_id, remove_message = __pack_message(2006, data) return request_id, remove_message, len(encoded)
def insert(collection_name, docs, check_keys, continue_on_error, opts): """Get an unacknowledged **insert** message. .. versionchanged:: 3.6 Removed 'safe' and 'last_error_args' arguments. """ options = 0 if continue_on_error: options += 1 data = struct.pack("<i", options) data += bson._make_c_string(collection_name) encoded = [bson.BSON.encode(doc, check_keys, opts) for doc in docs] if not encoded: raise InvalidOperation("cannot do an empty bulk insert") max_bson_size = max(map(len, encoded)) data += _EMPTY.join(encoded) request_id, insert_message = __pack_message(2002, data) return request_id, insert_message, max_bson_size
def insert(collection_name, docs, check_keys, safe, last_error_args): """Get an **insert** message. """ max_bson_size = 0 data = __ZERO data += bson._make_c_string(collection_name) encoded = [bson.BSON.encode(doc, check_keys) for doc in docs] if not encoded: raise InvalidOperation("cannot do an empty bulk insert") max_bson_size = max(map(len, encoded)) data += "".join(encoded) if safe: (_, insert_message) = __pack_message(2002, data) (request_id, error_message, _) = __last_error(last_error_args) return (request_id, insert_message + error_message, max_bson_size) else: (request_id, insert_message) = __pack_message(2002, data) return (request_id, insert_message, max_bson_size)
def update(collection_name, upsert, multi, spec, doc, safe): """Get an **update** message. """ options = 0 if upsert: options += 1 if multi: options += 2 data = __ZERO data += bson._make_c_string(collection_name) data += struct.pack("<i", options) data += bson.BSON.from_dict(spec) data += bson.BSON.from_dict(doc) if safe: (_, update_message) = __pack_message(2001, data) (request_id, error_message) = __last_error() return (request_id, update_message + error_message) else: return __pack_message(2001, data)
def delete(collection_name, spec, safe, last_error_args, opts, flags=0): """Get a **delete** message. `opts` is a CodecOptions. `flags` is a bit vector that may contain the SingleRemove flag or not: http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/#op-delete """ data = _ZERO_32 data += bson._make_c_string(collection_name) data += struct.pack("<I", flags) encoded = bson.BSON.encode(spec, False, opts) data += encoded if safe: (_, remove_message) = __pack_message(2006, data) (request_id, error_message, _) = __last_error(collection_name, last_error_args) return (request_id, remove_message + error_message, len(encoded)) else: (request_id, remove_message) = __pack_message(2006, data) return (request_id, remove_message, len(encoded))
def update(collection_name, upsert, multi, spec, doc, check_keys, opts): """Get an unacknowledged **update** message. .. versionchanged:: 3.6 Removed 'safe' and 'last_error_args' arguments. """ options = 0 if upsert: options += 1 if multi: options += 2 data = _ZERO_32 data += bson._make_c_string(collection_name) data += struct.pack("<i", options) data += bson.BSON.encode(spec, False, opts) encoded = bson.BSON.encode(doc, check_keys, opts) data += encoded request_id, update_message = __pack_message(2001, data) return request_id, update_message, len(encoded)