def get_more(collection_name, num_to_return, cursor_id): """Get a **getMore** message. """ data = __ZERO 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 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 delete(collection_name, spec, safe, last_error_args): """Get a **delete** message. """ data = __ZERO data += bson._make_c_string(collection_name) data += __ZERO data += bson.BSON.encode(spec) if safe: (_, remove_message) = __pack_message(2006, data) (request_id, error_message) = __last_error(last_error_args) return (request_id, remove_message + error_message) else: return __pack_message(2006, data)
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, safe, last_error_args): """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.encode(spec) data += bson.BSON.encode(doc) if safe: (_, update_message) = __pack_message(2001, data) (request_id, error_message) = __last_error(last_error_args) return (request_id, update_message + error_message) else: return __pack_message(2001, data)