gpt4 book ai didi

javascript对象转mongodb结构,不会冒泡

转载 作者:可可西里 更新时间:2023-11-01 09:55:47 25 4
gpt4 key购买 nike

所以为了进一步解释标题,我正在尝试将javascript 对象转换为MongoDB结构以进行查询。

大家可能知道,mongodb在查询数据库的时候是严格匹配的。换句话说,文档必须完全匹配。

例如,假设我们有一个具有以下布局的 mongo 文档:

{
location: {
city: "Some city",
state: "Some state",
color: {
red: "and yellow",
green: "and gold"
}
}
}


要查询城市,您可以执行以下操作:

{
"location.city": "Some city"
}

而不是:

{
"location": {
"city": "Some city"
}
}


这就是这个函数诞生的原因。下面这个函数好像没有?冒泡?结果向上对象。此外,结构中的旧对象也被复制。



函数:

function query_to_mongo(query, root) {
var new_query = {};

for (var property in query) {
if (!(typeof(query))[property]) {

if (!root) { // root object.
root = property;

// Property value is type of object.
if (typeof(query[property]) === 'object') {
new_query = query_to_mongo(query[property], property);
} else {
new_query[property] = query[property];
}

} else {

console.log('Last prop: ' + root + '.' + property);

// Property value is type of object.
if (typeof(query[property]) === 'object') {
new_query[root + '.' + property] = query_to_mongo(query[property], property);
} else {
new_query[root + '.' + property] = query[property];

}

}
}

return new_query;
}


代码:

var this_query = {
location: {
city: "Some city",
state: "Some state",
color: {
red: "and yellow",
green: "and gold"
}
}
};

console.log( query_to_mongo(this_query) );



结果:

{
location.city: "Some city",
location.color: [object Object] {
color.green: "and gold",
color.red: "and yellow"
},
location.state: "Some state"
}


预期:

{
location.city: "Some city",
location.color.green: "and gold",
location.color.red: "and yellow",
location.state: "Some state"
}



所以问题是,发生了什么?此外,是否有更好、更优雅的方式来满足我的需求?

最佳答案

所以,几个小时后,我终于想出了如何完成我的任务。此外,我还决定添加一张我将允许使用的运营商 map 。


代码:

function apply_operator(op) {
switch(op) {
case "$exists": return true;
case "$in": return true;
default: return false;
}
}

function query_to_mongo(query) {
var new_query = {};

(function(obj, val) {
val = val || '';

if (typeof(obj) !== 'object') return true;

for (var property in obj) {
if (arguments.callee(obj[property], val + "." + property)) {
if (apply_operator(property)) {
new_query[val.substring(1)] = JSON.parse('{"' + property + '": "' + obj[property] + '"}');
} else {
if (val.substring(1)) {
new_query[val.substring(1) + "." + property] = obj[property];
} else {
new_query[property] = obj[property];
}
}
}
}
return false;
})(query);

return new_query;
}

var this_query = {
location: {
city: "Some city",
state: "Some state",
color: {
red: "and yellow",
green: "and gold",
gold: {
best: "ever"
}
}
},
exister: { "$exists": true }
};

console.log( query_to_mongo(this_query) );



结果是:

[object Object] {
exister: [object Object] {
$exists: "true"
},
location.city: "Some city",
location.color.gold.best: "ever",
location.color.green: "and gold",
location.color.red: "and yellow",
location.state: "Some state"
}

关于javascript对象转mongodb结构,不会冒泡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21212648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com