mongodb query without field name

前端 未结 2 452
迷失自我
迷失自我 2020-12-18 14:55

I would like query all objects that have a field containing a specific value. For example, I have two documents:

{\"123\": \"apple\", \"217\": \"pear\", \"17         


        
2条回答
  •  一向
    一向 (楼主)
    2020-12-18 15:35

    You can use Full-text Index on all fields.

    use dbname
    db.collectionname.ensureIndex(
                               { "$**": "text" },
                               { name: "TextIndex" }
                             )
    

    Then Search Using :

    DB db = mongoClient.getDB("dbname");
    DBCollection coll = db.getCollection("collectionname");
    BasicDBObject query = new BasicDBObject();
                query.append("$text", new BasicDBObject("$search", searchstring));
                DBCursor cursor = coll.find(query);
    

提交回复
热议问题