Good day, I'm using new language Apple Swift and using the NSArray:
[
{
"Section" : "Title1",
"Items" :
[
{"Nr" : "101"},
{"Nr" : "201"},
{"Nr" : "301"},
{"Nr" : "401"}
]
},
{
"Section" : "Title2",
"Items" :
[
{"Nr" : "102"},
{"Nr" : "202"},
{"Nr" : "302"}
]
},
{
"Section" : "Title3",
"Items" :
[
{"Nr" : "1102"},
{"Nr" : "2102"},
{"Nr" : "3102"}
]
}
]
I want to use search, and display only content what is found
As I understand I have to use the "filteredArrayUsingPredicate" and NSPredicate in swift, so my sample is:
var arr:NSArray = [...] // My array sample goes here
var pre:NSPredicate = NSPredicate(format: "ANY Items.Nr BEGINSWITH[c] %#", argumentArray: ["20"]) // We a going to search "20" in /Items/Nr
var result:NSArray = arr.filteredArrayUsingPredicate(pre)
This is working correct but result is not what I need:
result =
[
{
"Section" : "Title1",
"Items" :
[
{"Nr" : "101"},
{"Nr" : "201"},
{"Nr" : "301"},
{"Nr" : "401"}
]
},
{
"Section" : "Title2",
"Items" :
[
{"Nr" : "102"},
{"Nr" : "202"},
{"Nr" : "302"}
]
}
]
It is display me all section what is contains Imtes with Nr started with "20"
And my question is how to do filter also in Items? The result what I need is have to be:
result =
[
{
"Section" : "Title1",
"Items" :
[
{"Nr" : "201"}
]
},
{
"Section" : "Title2",
"Items" :
[
{"Nr" : "202"}
]
}
]
I tried to use the SUBQUERY:
"SUBQUERY(Items, $x, $x.Nr BEGINSWITH[c] %#).#count > 0"
But this return me the same, if it can calculate the Items, then I thought that it can display me Items/Nr what I found, but I dont know how to write this correct. :)
Oh, hang on. I just re-read your question, editing now
To answer your requirement of filtering the sub arrays. Your data model is getting far too complex to still be using generic objects. You should think about setting up an actual data structure with custom objects. You can then offload a lot of the work to these objects instead of putting all the work in one place.
Just trying to find the best way to do this (without a custom data model).
P.S. make a custom data model :)
OK, here we go
I made this a lot easier by wrapping your generic data up into a struct...
struct Item {
let section: String
let items: [[String: String]]
}
It still feels wrong having an array of dictionaries where each dictionary has only one key and it's always the same. Really it should just be an array of strings.
Anyway...
Create the array like so...
let items: [Item] = [
Item(section: "Title1", items: [["Nr" : "101"], ["Nr" : "201"], ["Nr" : "301"], ["Nr" : "401"]]),
Item(section: "Title2", items: [["Nr" : "102"], ["Nr" : "202"], ["Nr" : "302"]]),
Item(section: "Title3", items: [["Nr" : "1102"], ["Nr" : "2102"], ["Nr" : "3102"]])
]
Or from your data however you have it stored.
Then filter and map it into a new array...
let filteredMappedArray = items.filter {
// this filters the array so that only its containing "20..." are in it.
for dictionary in $0.items {
if let numberString = dictionary["Nr"] {
if (numberString.hasPrefix("20")) {
return true
}
}
}
return false
}.map {
// This maps the array and removes anything that isn't "20..." from the items.
Item(section: $0.section, items: $0.items.filter {
numberDictionary in
if let numberString = numberDictionary["Nr"] {
return numberString.hasPrefix("20")
}
return false
})
}
Then I logged the result...
for item in filteredMappedArray {
println("Section: \(item.section) - Items: \(item.items)")
}
And got this result...
Section: Title1 - Items: [[Nr: 201]]
Section: Title2 - Items: [[Nr: 202]]
There may be better/simpler way to combine everything into one function but this is the easiest way I could find.
If you can change your dictionary array thing...
If item can be defined as...
struct Item {
let section: String
let items: [String]
}
Then the filter-map function would become...
let filteredMappedArray = items.filter {
// this filters the array so that only its containing "20..." are in it.
for numberString in $0.items {
if (numberString.hasPrefix("20")) {
return true
}
}
return false
}.map {
// This maps the array and removes anything that isn't "20..." from the items.
Item(section: $0.section, items: $0.items.filter {$0.hasPrefix("20")})
}
By changing that dictionary array to just an array of strings you're removing a layer of complexity that isn't necessary.
Related
I useGeofire for iOS. I faced the following problem, I use Geofire to get the cards and users that are next to the current user. For cards, the result is correct, but for users I do not get any results, although the data structure in Firebase Database is the same for storing the location of cards and users. Please tell me how can I solve this problem?
My structure data
"cardLocation" : {
"-KjLxK0q39JnT2MZEalB" : {
".priority" : "v17wefy8z",
"g" : "v17wefy8z",
"l" : [ *****, ****** ]
},
"-KjM7_5sPkoruwoTvRzR" : {
".priority" : "9q5c2ypf3",
"g" : "9q5c2ypf3",
"l" : [ ******, -****** ]
},
"-KjNDq8nQ2Ffjr9M_1a9" : {
".priority" : "9q59x2vc6",
"g" : "9q59x2vc6",
"l" : [ *******, -***** ]
}
},
"userLocations" : {
"1Cix149ThIOG1ULPVjyy0LyTxbe2" : {
".priority" : "87zc0d2j5",
"g" : "87zc0d2j5",
"l" : [ ******, -**** ]
},
I did not correctly use geofire by adding additional property to its model. Now I add only CLLocation to it and it works for me.
let geofireRef = Database.database().reference().child(MainGateways.cardLocation.rawValue)
guard let geoFire = GeoFire(firebaseRef: geofireRef) else { return }
dispatchGroup.enter()
geoFire.setLocation(cardLocation, forKey: cardModelRef.key) { (error) in
commonError = error
dispatchGroup.leave()
}
In MongoDB, I'm trying to write a query to add elements from an array to an existing document, but instead of adding the elements as objects:
property: ObjectID(xxx)
the elements are getting added as just
ObjectID(xxx)
Forgive me if I get the terminology wrong. I'm completely new to MongoDB; I normally only work with relational databases. How do I properly add these new elements?
I have a collection called auctions which has two fields: ID and properties. Properties is an array of objects named property. Here's an example with two auction documents:
** I changed the object IDs to make them easier to reference in our discussion
Collection db.auctions
{
"_id" : ObjectId("abc"),
"properties" : [
{
"property" : ObjectId("prop1")
},
{
"property" : ObjectId("prop2")
},
{
"property" : ObjectId("prop3")
}]
}
{
"_id" : ObjectId("def"),
"properties" : [
{
"property" : ObjectId("prop97")
},
{
"property" : ObjectId("prop98")
}]
}
I want to add 3 new properties to auction "abc". How do I do this?
Here's is what I tried:
I have an array of properties that looks like this:
Array PropsToAdd
[
ObjectId("prop4"),
ObjectId("prop5"),
ObjectId("prop6")
]
I wrote an update query to push these properties into the properties array in auctions:
db.auctions.update(
{"_id": "abc"}
,
{ $push: { properties: { $each: PropsToAdd } } }
);
This query gave the result below. Notice that instead of adding elements named property with a value from my array, it's just added my values from my array. I obviously need to add that "property" part, but how do I do that?
Collection db.auctions (_id "abc" only)
{
"_id" : ObjectId("abc"),
"properties" : [
{
"property" : ObjectId("prop1")
},
{
"property" : ObjectId("prop2")
},
{
"property" : ObjectId("prop3")
},
ObjectId("prop4"),
ObjectId("prop5"),
ObjectId("prop6"),
ObjectId("prop7")]
}
The result I'm looking for is this:
Collection db.auctions (_id "abc" only)
{
"_id" : ObjectId("abc"),
"properties" : [
{
"property" : ObjectId("prop1")
},
{
"property" : ObjectId("prop2")
},
{
"property" : ObjectId("prop3")
},
{
"property" : ObjectId("prop4")
},
{
"property" : ObjectId("prop5")
},
{
"property" : ObjectId("prop6")
}
}
Here is some further information on that array of properties I'm adding. I get it from running these queries. Perhaps one of them needs changed?
This query gets an array of current properties:
var oldActiveProperties = db.properties.distinct( "saleNumber", { "active": true, "auction": ObjectId("abc") } );
Then those results are used to find properties in the new file that weren't in the old file:
var PropsToAdd = db.newProperties.distinct(
"_id"
, { "saleNumber": { "$nin": oldActiveProperties }, "active": true}
);
The resulting array is what I need to add to the auctions collection.
Use the JavaScript's native map() method to map the array into an array of documents. The following shows this:
var PropsToAdd = db.newProperties.distinct("_id",
{ "saleNumber": { "$nin": oldActiveProperties }, "active": true}
).map(function (p) { return { property: p }; });
db.auctions.update(
{"_id": "abc"},
{ $push: { "properties": { "$each": PropsToAdd } } }
);
I need to set the result of a query as an array of an object being saved.
First I get the Items:
Item.find({...}, function...
which looks like that:
data =
[ { foo: [],
bar: true,
_id: 564f62b1659abdc22b9dd1da },
{ foo: [],
bar: true,
_id: 564f76b628bd4b62335bad7c } ]
Now I create an object which I want to save with the _ids of the query result as an "Item" ref array:
var itemholder = new Itemholder(req.body);
itemholder.items = ????
Itemholder has an array of "Item" refs called "items" which should look like this:
"items" : [ { "item" : ObjectId("564f62b1659abdc22b9dd1da"),
"_id" : ObjectId("564f54c4e9bbbbee230647a4") },
{ "item" : ObjectId("564f76b628bd4b62335bad7c"),
"_id" : ObjectId("564f67043f88a5b12d6c32ff") } ]
Thank you.
Found a way to do it (probably not the best):
Item.find(..., function(err, data){
if(err){
...
}else{
data = data.map(function(data) { return {'item': data._id}; });
sprint.items = data;
sprint.save(function(err2, data2){
...
This is the first of 7 test/example documents, in collection "SoManySins."
{
"_id" : ObjectId("51671bb6a6a02d7812000018"),
"Treats" : "Sin1 = Gluttony",
"Sin1" : "Gluttony",
"Favourited" : "YES",
"RecentActivity" : "YES",
"GoAgain?" : "YeaSure."
}
I would like to be able to query to retrieve any info in any position,
just by referring to the position. The following document,
{
"_id" : ObjectId("51671bb6a6a02d7812000018"),
"Sin1" : "Gluttony",
"?????????" : "??????",
"RecentActivity" : "YES",
"GoAgain?" : "YeaSure."
}
One could retrieve whatever might be in the 3rd key~value
pair. Why should one have to know ahead of time what the
data is, in the key? If one has the same structure for the
collection, who needs to know? This way, you can get
double the efficiency? Like having a whole lot of mailboxes,
and your app's users supply the key and the value; your app
just queries the dbs' documents' arrays' positions.
Clara? finally? I hope?
The sample document you've provided is not saved as an array in BSON:
{
"_id" : ObjectId("51671bb6a6a02d7812000018"),
"Sin1" : "Gluttony",
"?????????" : "??????",
"RecentActivity" : "YES",
"GoAgain?" : "YeaSure."
}
Depending on the MongoDB driver you are using, the fields here are typically represented in your application code as an associative array or hash. These data structures are not order-preserving so you cannot assume that the 3rd field in a given document will correspond to the same field in another document (or even that the same field ordering will be consistent on multiple fetches). You need to reference the field by name.
If you instead use an array for your fields, you can refer by position or select a subset of the array using the $slice projection.
Example document with an array of fields:
{
"_id" : ObjectId("51671bb6a6a02d7812000018"),
"fields": [
{ "Sin1" : "Gluttony" },
{ "?????????" : "??????" },
{ "RecentActivity" : "YES" },
{ "GoAgain?" : "YeaSure." }
]
}
.. and query to find the second element of the fields array (a $slice with skip 1, limit 1):
db.SoManySins.find({}, { fields: { $slice: [1,1]} })
{
"_id" : ObjectId("51671bb6a6a02d7812000018"),
"fields" : [
{
"?????????" : "??????"
}
]
}
This is one way to Query and get back data when you may not
know what the data is, but you know the structure of the data:
examples in Mongo Shell, and in PHP
// the basics, setup:
$dbhost = 'localhost'; $dbname = 'test';
$m = new Mongo("mongodb://$dbhost");
$db = $m->$dbname;
$CursorFerWrites = $db->NEWthang;
// defining a set of data, creating a document with PHP:
$TheFieldGenerator = array( 'FieldxExp' => array(
array('Doc1 K1'=>'Val A1','Doc1 K2'=>'ValA2','Doc1 K3'=>'Val A3'),
array('Doc2 K1'=>'V1','Doc2 K2'=>'V2','Doc2 K3'=>'V3' ) ) ) ;
// then write it to MongoDB:
$CursorFerWrites->save($TheFieldGenerator);
NOTE : In the Shell : This produces the same Document:
> db.NEWthang.insert({"FieldxExp" : [
{"Doc1 K1":"Val A1","Doc1 K2":"Val A2","Doc1 K3":"Val A3"},
{"Doc2 K1":"V1", "Doc2 K2":"V2","Doc2 K3":"V3"}
]
})
#
Now, some mongodb Shell syntax:
> db.NEWthang.find().pretty()
{
"_id" : ObjectId("516c4053baa133464d36e836"),
"FieldxExp" : [
{
"Doc1 K1" : "Val A1",
"Doc1 K2" : "Val A2",
"Doc1 K3" : "Val A3"
},
{
"Doc2 K1" : "V1",
"Doc2 K2" : "V2",
"Doc2 K3" : "V3"
}
]
}
> db.NEWthang.find({}, { "FieldxExp" : { $slice: [1,1]} } ).pretty()
{
"_id" : ObjectId("516c4053baa133464d36e836"),
"FieldxExp" : [
{
"Doc2 K1" : "V1",
"Doc2 K2" : "V2",
"Doc2 K3" : "V3"
}
]
}
> db.NEWthang.find({}, { "FieldxExp" : { $slice: [0,1]} } ).pretty()
{
"_id" : ObjectId("516c4053baa133464d36e836"),
"FieldxExp" : [
{
"Doc1 K1" : "Val A1",
"Doc1 K2" : "Val A2",
"Doc1 K3" : "Val A3"
}
]
}
Finally, how about write the Query in some PHP ::
// these will be for building the MongoCursor:
$myEmptyArray = array();
$TheProjectionCriteria = array('FieldxExp'=> array('$slice' => array(1,1)));
// which gets set up here:
$CursorNEWthang1 = new MongoCollection($db, 'NEWthang');
// and now ready to make the Query/read:
$ReadomgomgPls=$CursorNEWthang1->find($myEmptyArray,$TheProjectionCriteria);
and the second document will be printed out:
foreach ($ReadomgomgPls as $somekey=>$AxMongoDBxDocFromCollection) {
var_dump($AxMongoDBxDocFromCollection);echo '<br />';
}
Hope this is helpful for a few folks.
Similar to this question
Barrowing the data set, I have something similar to this:
{
'user_id':'{1231mjnD-32JIjn-3213}',
'name':'John',
'campaigns':
[
{
'campaign_id':3221,
'start_date':'12-01-2012',
},
{
'campaign_id':3222,
'start_date':'13-01-2012',
}
]
}
And I want to add a new key in the campaigns like so:
{
'user_id':'{1231mjnD-32JIjn-3213}',
'name':'John',
'campaigns':
[
{
'campaign_id':3221,
'start_date':'12-01-2012',
'worker_id': '00000'
},
{
'campaign_id':3222,
'start_date':'13-01-2012',
'worker_id': '00000'
}
]
}
How to insert/update a new key into an array of objects?
I want to add a new key into every object inside the array with a default value of 00000.
I have tried:
db.test.update({}, {$set: {'campaigns.worker_id': 00000}}, true, true)
db.test.update({}, {$set: {campaigns: {worker_id': 00000}}}, true, true)
Any suggestions?
I'm supposing that this operation will occur once, so you can use a script to handle it:
var docs = db.test.find();
for(var i in docs) {
var document = docs[i];
for(var j in document.campaigns) {
var campaign = document.campaigns[j];
campaign.worker_id = '00000';
}
db.test.save(document);
}
The script will iterate over all documents in your collection then over all campaigns in each document, setting the *worker_id* property.
At the end, each document is persisted.
db.test.update({}, {$set: {'campaigns.0.worker_id': 00000}}, true, true
this will update 0 element.
if you want to add a new key into every object inside the array you should use:
$unwind
example:
{
title : "this is my title" ,
author : "bob" ,
posted : new Date() ,
pageViews : 5 ,
tags : [ "fun" , "good" , "fun" ] ,
comments : [
{ author :"joe" , text : "this is cool" } ,
{ author :"sam" , text : "this is bad" }
],
other : { foo : 5 }
}
unwinding tags
db.article.aggregate(
{ $project : {
author : 1 ,
title : 1 ,
tags : 1
}},
{ $unwind : "$tags" }
);
result:
{
"result" : [
{
"_id" : ObjectId("4e6e4ef557b77501a49233f6"),
"title" : "this is my title",
"author" : "bob",
"tags" : "fun"
},
{
"_id" : ObjectId("4e6e4ef557b77501a49233f6"),
"title" : "this is my title",
"author" : "bob",
"tags" : "good"
},
{
"_id" : ObjectId("4e6e4ef557b77501a49233f6"),
"title" : "this is my title",
"author" : "bob",
"tags" : "fun"
}
],
"OK" : 1
}
After you could write simple updaiting query.