Using Binary Data datetype for storing byte arrays in mongoDB

Store GUIDs as BinData, rather than as strings

BSON includes a binary data datatype for storing byte arrays. Using this will make the id values, and their respective keys in the _id index, twice as small.

Note that unlike the BSON Object ID type (see above), most UUIDs do not have a rough ascending order, which creates additional caching needs for their index.

根据官网文档,以byte arrays的方式存储数据,特别是_id,能减少索引大小,提高效率,不过没有验证。目前的Java Driver对此支持好像不是很好,其Binary类可以把数据存入mongodb,但是读取字段时却不能进行强制转换回来。

String t = "123456789abcdef9";

Binary id = new Binary(t.getBytes());

BasicDBObject obj = new BasicDBObject();

obj.append("_id", id).append("name", "me");

coll.insert(obj);
System.out.println(coll.find(obj).explain());

DBCursor cursor = coll.find(obj);

while(cursor.hasNext())

{

DBObject o = cursor.next();

System.out.println(o);

Binary b = (Binary) o.get("_id"); // won't work, will get Exception when doing data type convertion

System.out.println(new String((byte[])o.get("_id"))); // this way works well

// System.out.println(k);

}

To express SOS1 variables using binary variables in MATLAB, you can use the following steps: 1. Create binary variables for each of the original variables in your SOS1 set. For example, if your SOS1 set has variables x1, x2, x3, then you would create binary variables y1, y2, y3. 2. Add the following constraints to your model: a. For each pair of binary variables yi and yj, add the constraint yi + yj <= 1. This ensures that at most one binary variable can take the value of 1. b. For each original variable xi and its corresponding binary variable yi, add the constraint xi <= yi. This ensures that if the binary variable is set to 0, then the original variable must also be 0. c. For each pair of adjacent variables in the SOS1 set, xi and xj, and their corresponding binary variables, yi and yj, add the constraint yi + yj >= xj. This ensures that if xj is non-zero, then at least one of the corresponding binary variables must also be non-zero. 3. Add your objective function and any additional constraints to your model as needed. 4. Solve the model using a solver such as MATLAB's built-in linprog function or an external solver. Here is an example code snippet that demonstrates how to express SOS1 variables using binary variables in MATLAB: ``` % Define original variables x = [5; 2; 3; 1]; % Create binary variables y = binvar(4,1); % Add constraints constraints = [y(1) + y(2) + y(3) + y(4) == 1]; for i = 1:4 constraints = [constraints, y(i) <= x(i)]; end for i = 1:3 constraints = [constraints, y(i) + y(i+1) >= x(i+1)]; end for i = 1:4 for j = 1:4 if i ~= j constraints = [constraints, y(i) + y(j) <= 1]; end end end % Define objective function f = [1; 1; 1; 1]; % Solve the model [x_opt, fval] = linprog(f, [], [], constraints, [], zeros(4,1)); ``` In this example, we have a set of four original variables (x1, x2, x3, x4) and we create binary variables (y1, y2, y3, y4) for each of them. We then add the constraints described above and define an objective function. Finally, we solve the model using the linprog function in MATLAB.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值