mongodb-query

How to search word or string on all fields in document with MongoDB?

我与影子孤独终老i 提交于 2019-12-24 02:13:09
问题 We have a situation where a user would like to have a single 'omni-box' for searching for a word\phrase anywhere in a document. Is MongoDB able to perform this search or would it have to be an explicit search of every field? 回答1: You need to create a wildcard text indexes like this: db.collection.createIndex( { "$**": "text" } ) You can the use the $text operator to performs a text search. As mentioned in the documentation: This index allows for text search on all fields with string content.

Is it possible to pullAll on multiple matched results

↘锁芯ラ 提交于 2019-12-24 00:59:30
问题 I have following documents: { _id: 1 items: ['1a', '1b', '1c'] }, { _id: 2, items: ['2a', '2b', '2c'] } Is it possible to pull items from these documents but only if all the items exist? To explain: I need to pull '1a', '1c' and '2b' elemnets from these documents, so I if issue the query like the following: var updateObj = ['1a', '1c', '2b']; db.collection.update({_id : {$in updateObj}}, {$pullAll: {items: updateObj}}); Than will return success and pull the items. However if issue the

Linq to MongoDB Filter

こ雲淡風輕ζ 提交于 2019-12-24 00:58:24
问题 I have a MongoDB collection, listed below: { "_id" : ObjectId("001"), "ticker" : "MSFT=US", "exchange" : "OTC", "localtick" : "MSFT", "compname" : "Microsoft", "currency" : "USD", "insertedtime" : ISODate("2016-06-13T23:10:09.341+0000") } { "_id" : ObjectId("002"), "ticker" : "TSLA=CA", "exchange" : "TSX", "localtick" : "TSLA", , "compname" : "Tesla", "currency" : "CAD", "insertedtime" : ISODate("2016-06-13T23:10:09.809+0000") } But when I try to do a filter in my query: var documents =

Need a distinct count on multiple fields that were joined from another collection using mongodb aggregation query

风流意气都作罢 提交于 2019-12-24 00:48:45
问题 I'm trying to use a mongodb aggregation query to join($lookup) two collections and then distinct count all the unique values in the joined array. So my two collections look like this: events- { "_id" : "1", "name" : "event1", "objectsIds" : [ "1", "2", "3" ], } Objects { "_id" : "1", "name" : "object1", "metaDataMap" : { "SOURCE" : ["ABC", "DEF"], "DESTINATION" : ["XYZ", "PDQ"], "TYPE" : [] } }, { "_id" : "2", "name" : "object2", "metaDataMap" : { "SOURCE" : ["RST", "LNE"], "TYPE" : ["text"]

The Foreign field of $lookup could be the field of nested document?

*爱你&永不变心* 提交于 2019-12-24 00:38:00
问题 $lookup is used to perform a left outer join to an unsharded collection in the same database to filter in documents from the “joined” collection for processing in Mongo. { $lookup: { from: <collection to join>, localField: <field from the input documents>, foreignField: <field from the documents of the "from" collection>, as: <output array field> } } Could the foreignField be the field of the nested document of from collection? For example, there are two collections as following. history

How to Update Multiple Array objects in mongodb

怎甘沉沦 提交于 2019-12-24 00:17:20
问题 How to Update Multiple Array objects in mongodb. This question was asked earlier - How to Update Multiple Array elements in mongodb But that didn't work for me. I have an array of objects { "_id" : ObjectId("4d2d8deff4e6c1d71fc29a07"), "user_id" : "714638ba-2e08-2168-2b99-00002f3d43c0", "events" : [ { "handled" : { "name": "Mike", "visibile": false }, "profile" : 10, "data" : "....." } { "handled" : { "name": "Shaun", "visibile": false }, "profile" : 10, "data" : "....." } { "handled" : {

Find documents where ALL elements of an array have a specific value

元气小坏坏 提交于 2019-12-23 23:49:34
问题 This is basically a simple problem, but I couldn't find a query function for it. Example Collection: { _id: 1, foo: [ { bar: 9 }, { bar: 16 } ] } { _id: 2, foo: [ { bar: 9 }, { bar: 9 }, { bar: 9 } ] } Example Output: { _id: 2, foo: [ { bar: 9 }, { bar: 9 }, { bar: 9 } ] } Because this is the only document where every foo.bar = 9. The Query I'm looking for: "FIND all documents WHERE foo.bar = 9 FOR EVERY foo.bar in this document." Or do I need something like "FIND all documents WHERE NOT( foo

How to $setDifference in array & Object using Mongo DB

别来无恙 提交于 2019-12-23 23:12:32
问题 UserDetails { "_id" : "5c23536f807caa1bec00e79b", "UID" : "1", "name" : "A", }, { "_id" : "5c23536f807caa1bec00e78b", "UID" : "2", "name" : "B", }, { "_id" : "5c23536f807caa1bec00e90", "UID" : "3", "name" : "C" } UserProducts { "_id" : "5c23536f807caa1bec00e79c", "UPID" : "100", "UID" : "1", "status" : "A" }, { "_id" : "5c23536f807caa1bec00e79c", "UPID" : "200", "UID" : "2", "status" : "A" }, { "_id" : "5c23536f807caa1bec00e52c", "UPID" : "300", "UID" : "3", "status" : "A" } Groups { "_id" :

Updating database from a list of dictionaries

谁说我不能喝 提交于 2019-12-23 22:19:05
问题 In Python, I have a list of dictionaries. The list is called members and each member has a unique id . For example, the list could look like this: members = [{'id':1, 'val1':10, 'val2':11}, {'id':2, 'val1':2, 'val2':34}, {'id':3, 'val1':350, 'val2':9}] I want to update my collection with the list of members, updating and inserting new entries as necessary. Do I need to loop through the members, or is there a faster way? Here's my attempt, which seems to do what I want but takes a while: for m

Get elements of an array in date range

让人想犯罪 __ 提交于 2019-12-23 22:13:34
问题 This is an example document in my MongoDB collection: var user: [{ "_id" : ObjectId("5391b11a3c0a9ac01300006d"), "injurydata" : { "injuryinformation" : [ { "status" : "current", "updateddate" : ISODate("2014-06-06T12:16:20.306Z"), "updatedby" : "", "dateofinjury" : ISODate("2014-06-27T18:30:00.000Z"), "_id" : ObjectId("5391b11a3c0a9ac01300006f") } ] } }, { "_id" : ObjectId("5391b11a3c0a9ac01300006d"), "injurydata" : { "injuryinformation" : [ { "status" : "current", "updateddate" : ISODate(