MongoDB javascript查询;筛选列数组中的值

时间:2019-04-05 14:06:53

标签: javascript mongodb

使用香草javascript,我正在使用Mongo的dpd.js来查询数据库。

在此代码段中,我尝试在集合"design"的列roleConsiderations中找到techniques的所有匹配项。

roleConsiderations中的值在数组中-["development", "design", "content"],所有我尝试查询"design"的尝试都失败了。

dpd.js查询导致响应为空:

var query = {"roleConsiderations": "design"};
dpd.techniques.get(query, function (result) {
    console.log(result);
});

尝试通过url查询只会返回完全匹配的内容(其中"design"是数组中的唯一值)。

http://my.mongo.db/techniques?roleConsiderations=["design"]

那么如何在mongodb列过滤器中查询数组中的值?

1 个答案:

答案 0 :(得分:2)

您可以使用 $ in 运算符在指定字段中指定要包含返回的对象的元素。

def login():
    Username='' 
    Password=''
    counter=0
    for line in open("Credentials.txt","r").readlines(): # Read the lines
        login_info = line.split() # Split on the space, and store the results in a list of two strings
        while Username != login_info[0] and Password != login_info[1]: #If the username and password do not match the string elements from the txt file, starts the loop 
            while not (Username == login_info[0] and Password == login_info[1]):
                Username = input("Please enter your username: " ) #get elements from the user
                Password = input("Please enter your password: ")
            print("welcome")
        break

login() # call the function

我知道语法很糟糕,但是应该可以。

有关更多阅读信息,请参阅this