mongodb:将值数组返回到变量中

时间:2015-09-29 13:28:20

标签: mongodb mongodb-query

我来自RDBMS背景,发现很难让自己与mongodb保持一致,并且一直在努力只生成值数组。一旦我得到数组,我需要使用$ in

在聚合函数的$ condition子句中使用这些数组值

我有一个回复我的集合:

09-29 20:58:51.403: D/Databsse operations(797): Database created
09-29 20:58:53.202: D/Databsse operations(797): One row inserted
09-29 20:58:54.271: D/gralloc_goldfish(797): Emulator without GPU emulation detected.
09-29 21:00:07.152: D/dalvikvm(797): GC_CONCURRENT freed 67K, 7% free 2719K/2908K, paused 82ms+22ms, total 561ms
09-29 21:00:07.492: I/Choreographer(797): Skipped 53 frames!  The application may be doing too much work on its main thread.
09-29 21:00:26.392: D/Databsse operations(797): Database created
09-29 21:00:26.501: D/Databsse operations(797): Table combed
09-29 21:00:26.722: I/Choreographer(797): Skipped 43 frames!  The application may be doing too much work on its main thread.
09-29 21:00:27.552: D/dalvikvm(797): GC_FOR_ALLOC freed 187K, 10% free 2894K/3204K, paused 111ms, total 122ms
09-29 21:00:27.602: I/dalvikvm-heap(797): Grow heap (frag case) to 3.547MB for 635812-byte allocation
09-29 21:00:27.893: D/dalvikvm(797): GC_FOR_ALLOC freed 2K, 9% free 3512K/3828K, paused 294ms, total 295ms
09-29 21:00:28.192: D/dalvikvm(797): GC_CONCURRENT freed 34K, 9% free 3512K/3828K, paused 9ms+65ms, total 300ms
09-29 21:00:29.062: D/AndroidRuntime(797): Shutting down VM
09-29 21:00:29.082: W/dalvikvm(797): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-29 21:00:29.272: E/AndroidRuntime(797): FATAL EXCEPTION: main
09-29 21:00:29.272: E/AndroidRuntime(797): java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
09-29 21:00:29.272: E/AndroidRuntime(797):  at com.example.databank.NoteFragment.onCreateView(NoteFragment.java:17)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.support.v4.view.ViewPager.populate(ViewPager.java:1011)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.view.View.measure(View.java:15518)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.view.View.measure(View.java:15518)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.view.View.measure(View.java:15518)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-29 21:00:29.272: E/AndroidRuntime(797):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.view.View.measure(View.java:15518)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.view.Choreographer.doFrame(Choreographer.java:532)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.os.Handler.handleCallback(Handler.java:725)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.os.Looper.loop(Looper.java:137)
09-29 21:00:29.272: E/AndroidRuntime(797):  at android.app.ActivityThread.main(ActivityThread.java:5041)
09-29 21:00:29.272: E/AndroidRuntime(797):  at java.lang.reflect.Method.invokeNative(Native Method)
09-29 21:00:29.272: E/AndroidRuntime(797):  at java.lang.reflect.Method.invoke(Method.java:511)
09-29 21:00:29.272: E/AndroidRuntime(797):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-29 21:00:29.272: E/AndroidRuntime(797):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-29 21:00:29.272: E/AndroidRuntime(797):  at dalvik.system.NativeStart.main(Native Method)

内容:

db.Users.find({user_id : 1623}, { "Friends" : 1, _id : 0})

我只需要值数组而不是文档类型:

{ "Friends" : [ 708, 784, 1495, 212, 1918, 2007, 1439, 1634, 649 ] }

有没有更简单的方法来实现这一目标。

2 个答案:

答案 0 :(得分:1)

使用.distinct()代替,因为它基本上只返回一个数组:

db.Users.distinct("Friends", { "user_id": 1623 })

但我真的认为你是一个迂腐的人,应该接受结果"按原样#34;并改为使用数组作为字段。

答案 1 :(得分:0)

您可以直接询问Friends数组。即:假设user_id是唯一的:

import java.io.*;
import java.util.*;
class  Program108b {
  public static void main(String[] args) {
    int [] numbers = {1, 2, 3, 4};
    for(int x: numbers){
      System.out.println("run #" + x);    /// Add this
      Scanner kb = new Scanner(System.in);

      System.out.print("  Enter price ");
      double price = kb.nextDouble();

      System.out.print("Enter salvage value ");
      double salValue = kb.nextDouble();

      System.out.print("Enter estimated life in years ");
      int years = kb.nextInt();

      double anws = ((price- salValue)/years);
      System.out.print("Here is the depreciated value: " + (Math.round((anws)*100)/100.0));
    }
  }
}